TCP.Mod 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668
  1. (* Aos, Copyright 2001, Pieter Muller, ETH Zurich *)
  2. MODULE TCP; (** AUTHOR "pjm, mvt"; PURPOSE "TCP protocol"; *)
  3. (*
  4. TCP - Transmission Control Protocol. Based on the 4.4BSD-Lite distribution described in Wright and Stevens, "TCP/IP Illustrated, Volume 2: The Implementation", ISBN 0-201-63354-X. See the BSD copyright statement at the end of this module. From that code it inherits some horrible control flow, which was left mostly intact, to make it easier to compare with the book.
  5. TCP Header
  6. 00 16 source port
  7. 02 16 destination port
  8. 04 32 sequence number
  9. 08 32 acknowledgement number
  10. 12 08 header length & reserved
  11. 07..04 header length (4-byte units)
  12. 03..00 reserved
  13. 13 08 flags
  14. 07..06 reserved
  15. 05..05 URG
  16. 04..04 ACK
  17. 03..03 PSH
  18. 02..02 RST
  19. 01..01 SYN
  20. 00..00 FIN
  21. 14 16 window size
  22. 16 16 TCP checksum
  23. 18 16 urgent pointer
  24. 20 -- options (0-40 bytes)
  25. -- -- data
  26. TCP Pseudo-header (for checksum calculation)
  27. 00 32 source address
  28. 04 32 destination address
  29. 08 08 zero = 0
  30. 09 08 protocol = 17
  31. 10 16 TCP length (duplicate)
  32. Notes:
  33. o Bit numbers above are Intel bit order.
  34. o Avoid use of SET because of PPC bit numbering issues.
  35. o Always access fields as 8-, 16- or 32-bit values and use DIV, MOD, ASH, ODD for bit access.
  36. *)
  37. IMPORT SYSTEM, Machine, KernelLog, Clock, Modules, Objects, Kernel, Commands, Network, IP, Streams, ActiveTimers;
  38. CONST
  39. StrongChecks = FALSE;
  40. SystemMove = FALSE;
  41. TraceProtocol = FALSE;
  42. TraceError = FALSE;
  43. TracePacket = FALSE;
  44. TraceTimer = FALSE;
  45. TraceCongestion = FALSE;
  46. Trace = TraceProtocol OR TraceError OR TracePacket OR TraceTimer;
  47. HandleCongestion = TRUE;
  48. MinEphemeralPort = 1024;
  49. MaxEphemeralPort = 50000;
  50. HashTableSize = 1024 * 16; (* size of connection lookup hash table *)
  51. Acceptable = 500; (* "backlog" on listening connections *)
  52. NilPort* = 0;
  53. (** Error codes *)
  54. Ok* = 0;
  55. ConnectionRefused* = 3701;
  56. ConnectionReset* = 3702;
  57. WrongInterface* = 3703;
  58. TimedOut* = 3704;
  59. NotConnected* = 3705;
  60. NoInterface* = 3706;
  61. InterfaceClosed* = 3707;
  62. MinError = 3700;
  63. MaxError = 3735;
  64. NumErrors = MaxError-MinError+1;
  65. (** TCP connection states *)
  66. NumStates* = 12;
  67. Closed* = 0;
  68. Listen* = 1;
  69. SynSent* = 2;
  70. SynReceived* = 3;
  71. Established* = 4;
  72. CloseWait* = 5;
  73. FinWait1* = 6;
  74. Closing* = 7;
  75. LastAck* = 8;
  76. FinWait2* = 9;
  77. TimeWait* = 10;
  78. Unused* = 11; (* no real state, only used in this implementation *)
  79. OpenStates* = {Listen, SynReceived, Established, CloseWait, FinWait1, FinWait2};
  80. ClosedStates* = {Unused, Closed, Closing, LastAck, TimeWait};
  81. HalfClosedStates* = ClosedStates + {FinWait1, FinWait2};
  82. FinStates* = {Unused, Closed, CloseWait, Closing, LastAck, TimeWait};
  83. Fin = 0; Syn = 1; Rst = 2; Psh = 3; Ack = 4; Urg = 5; (* tcp header flags *)
  84. DoRFC1323 = TRUE; (* handle time stamp option (processing and generating) *)
  85. ProcOptions = TRUE; (* process TCP options *)
  86. GenOptions = TRUE; (* generate TCP options *)
  87. (* Flags in Connection *)
  88. AckNow = 0; (* send Ack immediately *)
  89. DelAck = 1; (* send Ack, but try to delay it *)
  90. NoDelay = 2; (* don't delay packets tocoalesce (disable Nagle algorithm) *)
  91. SentFin = 3; (* have sent Fin *)
  92. Force = 4; (* force out a byte (persist/OOB) *)
  93. RcvdScale = 5; (* set when other side sends window scale option in Syn *)
  94. RcvdTstmp = 6; (* set when other side sends timestamp option in Syn *)
  95. ReqScale = 7; (* have/will request window scale option in Syn *)
  96. ReqTstmp = 8; (* have/will request timestamp option in Syn *)
  97. DoKeepAlive = 9; (* enable keep-alive timer *)
  98. AcceptConn = 10; (* listening for incoming connections *)
  99. (*Notify = 11;*) (* socket wakeup *)
  100. (*Gone = 12;*) (* SS_NOFDREF *)
  101. (*NoMore = 13;*) (* SS_CANTRCVMORE *)
  102. Timeout = 14;
  103. NumTimers = 4;
  104. ReXmt = 0; Persist = 1; Keep = 2; MSL2 = 3;
  105. FastPeriod = 5; (* number of fast ticks per second *)
  106. SlowPeriod = 2; (* number of slow ticks per second *)
  107. TimerPeriod = 10; (* timer ticks per second *)
  108. MinTime = 1*SlowPeriod; (* minimum allowable time value *)
  109. ReXmtMax = 64*SlowPeriod; (* max allowable ReXmt value *)
  110. ReXmtThresh = 3;
  111. KeepInit = 75*SlowPeriod; (* connection establishment timer value (75s) *)
  112. KeepIntvl = 75*SlowPeriod; (* time between probes when no response (75s) *)
  113. KeepIdle = 2*60*60*SlowPeriod; (* default time before probing (2h) *)
  114. KeepCnt = 8; (* max probes before drop *)
  115. MaxIdle = KeepCnt * KeepIntvl; (* max time to send keepalive probes (10min) *)
  116. MSL = 30*SlowPeriod; (* max segment lifetime (30s) *)
  117. MaxPersistIdle = KeepIdle; (* max time to keep dead/unreachable connections (2h) *)
  118. PawsIdle = 24*24*60*60*SlowPeriod;
  119. SRTTBase = 0; (* base round trip time *)
  120. SRTTDflt = 3*SlowPeriod; (* assumed RTT if no info *)
  121. RTTShift = 3;
  122. RTTVarShift = 2;
  123. PersMin = 5*SlowPeriod; (* retransmit persistance *)
  124. PersMax = 60*SlowPeriod; (* maximum persist interval *)
  125. MSS = 536-12; (* maximum segment size for outgoing segments, 12 = size of timestamp option *)
  126. MaxRxtShift = 12; (* maximum retransmits *)
  127. MaxWin = 65535; (* largest value for (unscaled) window *)
  128. MaxWinShift = 14; (* maximum window shift *)
  129. MaxSendSpace = 80000H; (* 512KB, max. 1023MB *)
  130. MaxRecvSpace = 80000H; (* 512KB, max. 1023MB *)
  131. SegsPerBuf = 4; (* number of mss segments per send buffer (potential fragmentation waste is 1/SegsPerBuf) *)
  132. ISSInc = 128000; (* increment for iss each second *)
  133. IPTypeTCP = 6; (* TCP type code for IP packets *)
  134. MinTCPHdrLen = 20;
  135. MaxTCPHdrLen = 60;
  136. MaxPseudoHdrLen = 40; (* IPv4 = 12; IPv6 = 40 *)
  137. NewZeros = FALSE; (* NEW initializes allocated object fields to 0 *)
  138. BroadcastReceived = 3708;
  139. InvalidParameter = 3709;
  140. AllPortsInUse = 3710;
  141. AddressInUse = 3711;
  142. DuplicateSegment = 3712;
  143. DuplicatePartialSegment = 3713;
  144. DuplicateSegmentPAWS = 3714;
  145. DataBeyondWindow1 = 3715;
  146. DataBeyondWindow2 = 3716;
  147. DataBeyondWindow3 = 3717;
  148. BadChecksum = 3718;
  149. DuplicateAck = 3719;
  150. OutOfRangeAck = 3720;
  151. TimeOutKeepAlive = 3721;
  152. TimeoutEstablished = 3722;
  153. SegmentTooBig = 3723;
  154. SegmentTooSmall = 3724;
  155. BadHeaderLength = 3725;
  156. ConnectionGone = 3726;
  157. NIYNewIncarnation = 3727;
  158. NIYOutOfBand = 3728;
  159. NIYMSS = 3729;
  160. ConnectionAborted = 3730;
  161. NotInitialized = 3731;
  162. DataDuplicatePrevComplete = 3732;
  163. DataDuplicatePrevPartial = 3733;
  164. DataDuplicateNextComplete = 3734;
  165. DataDuplicateNextPartial = 3735;
  166. TYPE
  167. (* Send buffer types *)
  168. SendData = IP.Packet;
  169. SendBuffer = POINTER TO RECORD
  170. next: SendBuffer;
  171. ofs, len: LONGINT; (* data[ofs..ofs+len-1] is valid *)
  172. seq: LONGINT; (* sequence number of byte data[ofs] (only valid if len # 0) *)
  173. pf: SET; (* flags of segment *)
  174. data: SendData (* size should be multiple of maxseg *)
  175. END;
  176. TYPE
  177. ISS = OBJECT
  178. VAR iss: LONGINT; (* next iss to use *)
  179. PROCEDURE Update(hz: LONGINT);
  180. BEGIN {EXCLUSIVE}
  181. INC(iss, ISSInc DIV hz)
  182. END Update;
  183. PROCEDURE Get(): LONGINT;
  184. VAR t: LONGINT;
  185. BEGIN {EXCLUSIVE}
  186. t := iss; INC(iss, ISSInc);
  187. RETURN t
  188. END Get;
  189. PROCEDURE &Init*(iss: LONGINT);
  190. BEGIN
  191. SELF.iss := iss
  192. END Init;
  193. END ISS;
  194. TYPE
  195. Timer = OBJECT (* temporary *)
  196. VAR
  197. lastFast, lastSlow: LONGINT; (* time of last execution *)
  198. (*lastTrace: LONGINT;*)
  199. now: LONGINT; (* current tcp "time" - read from other procedures, but only updated inside this object *)
  200. timer: ActiveTimers.Timer;
  201. PROCEDURE CallDelayedAck(p: Connection);
  202. BEGIN
  203. p.DelayedAck();
  204. END CallDelayedAck;
  205. PROCEDURE CallSlowTimer(p: Connection);
  206. BEGIN
  207. p.SlowTimer();
  208. END CallSlowTimer;
  209. PROCEDURE HandleTimeout;
  210. VAR t: LONGINT;
  211. BEGIN {EXCLUSIVE}
  212. t := Kernel.GetTicks();
  213. IF t - lastFast >= Kernel.second DIV FastPeriod THEN
  214. lastFast := t;
  215. pool.Enumerate(CallDelayedAck);
  216. END;
  217. IF t - lastSlow >= Kernel.second DIV SlowPeriod THEN
  218. lastSlow := t;
  219. pool.Enumerate(CallSlowTimer);
  220. issSource.Update(SlowPeriod);
  221. INC(now)
  222. END;
  223. timer.SetTimeout(HandleTimeout, Kernel.second DIV TimerPeriod)
  224. END HandleTimeout;
  225. (* Finalize timer by cancelling it *)
  226. PROCEDURE Finalize;
  227. BEGIN {EXCLUSIVE}
  228. timer.Finalize
  229. END Finalize;
  230. PROCEDURE &Init*;
  231. BEGIN
  232. now := 0;
  233. lastSlow := Kernel.GetTicks() - Kernel.second;
  234. lastFast := lastSlow; (*lastTrace := lastSlow;*)
  235. NEW(timer);
  236. timer.SetTimeout(HandleTimeout, Kernel.second DIV TimerPeriod)
  237. END Init;
  238. END Timer;
  239. TYPE
  240. (** Connection object.
  241. NOTE: Only one process should access a Connection!
  242. *)
  243. Connection* = OBJECT(Streams.Connection)
  244. VAR
  245. poolNext, parent, acceptNext: Connection;
  246. (* assigned interface *)
  247. int-: IP.Interface;
  248. (* local protocol address *)
  249. lport-: LONGINT;
  250. (* foreign protocol address *)
  251. fip-: IP.Adr;
  252. fport-: LONGINT;
  253. state*: SHORTINT; (* TCP state *)
  254. timer: ARRAY NumTimers OF LONGINT;
  255. rxtshift-: LONGINT; (* log(2) of rexmt exponential backoff *)
  256. rxtcur-: LONGINT; (* current retransmission timeout (ticks) *)
  257. dupacks-: LONGINT; (* number of consequtive duplicate acks received *)
  258. maxseg-: LONGINT; (* maximum segment size to send *)
  259. flags: SET; (* various connection and buffer flags *)
  260. error: LONGINT; (* error on connection (socket error) *)
  261. acceptable: LONGINT; (* number of connections that can be before acceptance *)
  262. (* send sequence *)
  263. snduna-: LONGINT; (* send unacknowledged *)
  264. sndnxt-: LONGINT; (* send next *)
  265. sndup: LONGINT; (* send urgent pointer *)
  266. sndwl1-: LONGINT; (* window update seg seq number *)
  267. sndwl2-: LONGINT; (* window update seg ack number *)
  268. iss-: LONGINT; (* initial send sequence number *)
  269. sndwnd-: LONGINT; (* send window *)
  270. sndmax-: LONGINT; (* highest sequence number sent - used to recognize retransmits *)
  271. (* receive sequence *)
  272. rcvwnd-: LONGINT; (* receive window *)
  273. rcvnxt-: LONGINT; (* receive next *)
  274. rcvup: LONGINT; (* receive urgent pointer *)
  275. irs-: LONGINT; (* initial receive sequence number *)
  276. rcvadv-: LONGINT; (* advertised window by other end *)
  277. (* congestion control *)
  278. sndcwnd-: LONGINT; (* congestion-controlled window *)
  279. sndssthresh-: LONGINT; (* sndcwnd threshold for slow start - exponential to linear switch *)
  280. (* transmit timing *)
  281. idle-: LONGINT; (* inactivity time *)
  282. rtt-: LONGINT; (* round trip time *)
  283. rtseq-: LONGINT; (* sequence number being timed *)
  284. srtt-: LONGINT; (* smoothed round trip time *)
  285. rttvar-: LONGINT; (* variance in round trip time *)
  286. rttmin-: LONGINT; (* minimum rtt allowed *)
  287. maxsndwnd: LONGINT; (* largest window peer has offered *)
  288. (* RFC 1323 *)
  289. sndscale: LONGINT; (* scaling for send window (0-14) *)
  290. rcvscale: LONGINT; (* scaling for receive window (0-14) *)
  291. requestrscale: LONGINT; (* our pending window scale *)
  292. requestedsscale: LONGINT; (* peer's pending window scale *)
  293. tsrecent: LONGINT; (* timestamp echo data *)
  294. tsrecentage: LONGINT; (* when last updated *)
  295. lastacksent-: LONGINT; (* sequence number of last ack field *)
  296. (* send buffer *)
  297. sndcc-: LONGINT; (* number of bytes in send buffer *)
  298. sndspace-: LONGINT; (* number of bytes that may still be added before buffer is full *)
  299. sndhead, sndtail: SendBuffer; (* queue of segments (contiguous and in order) *)
  300. sndcontig: SendData; (* maxseg size buffer to make data contiguous *)
  301. (* receive buffer *)
  302. rcvspace-: LONGINT; (* number of bytes that may still be received before buffer is considered full *)
  303. rcvhiwat-: LONGINT; (* receive high water mark (MaxRecvSpace) *)
  304. rcvhead, rcvreasm, rcvtail: Network.Buffer; (* queue of segments - see description at the beginning of this file *)
  305. rcvheadFragment: Network.Buffer; (* current fragment of rcvhead *)
  306. timeout: ActiveTimers.Timer;
  307. traceflow-: LONGINT;
  308. (* Initialization for internal use only. *)
  309. PROCEDURE &Init*;
  310. BEGIN
  311. state := Unused;
  312. END Init;
  313. (** Open a TCP connection (only use once per Connection instance).
  314. Use TCP.NilPort for lport to automatically assign an unused local port.
  315. *)
  316. PROCEDURE Open*(lport: LONGINT; fip: IP.Adr; fport: LONGINT; VAR res: WORD);
  317. BEGIN {EXCLUSIVE}
  318. ASSERT((state = Unused) & (lport >= 0) & (lport < 10000H) & (fport >= 0) & (fport < 10000H));
  319. IF timeSource # NIL THEN
  320. InitConnection(SELF);
  321. IF (~IP.IsNilAdr(fip)) & (fport # NilPort) THEN
  322. (* active open (connect) *)
  323. int := IP.InterfaceByDstIP(fip);
  324. IF int # NIL THEN
  325. SELF.fip := fip;
  326. pool.Add(SELF, lport, fport, res); (* add connection to connection pool *)
  327. IF res = Ok THEN (* address assignment ok, now start the connection *)
  328. Machine.AtomicInc(NTCPConnectAttempt);
  329. state := SynSent; timer[Keep] := KeepInit;
  330. iss := issSource.Get();
  331. snduna := iss; sndnxt := iss; sndmax := iss; sndup := iss;
  332. Output(SELF)
  333. END;
  334. ELSE
  335. res := NoInterface;
  336. END;
  337. ELSE
  338. (* passive open (listen) *)
  339. ASSERT((fport = NilPort) & (IP.IsNilAdr(fip)));
  340. SELF.int := NIL;
  341. SELF.fip := IP.NilAdr;
  342. pool.Add(SELF, lport, NilPort, res);
  343. IF res = Ok THEN
  344. INCL(flags, AcceptConn);
  345. acceptable := Acceptable;
  346. state := Listen;
  347. END
  348. END;
  349. IF TraceProtocol THEN
  350. TraceTCP("Open", SELF, empty^, empty^, 0, 0, 0)
  351. END
  352. ELSE
  353. res := NotInitialized;
  354. END
  355. END Open;
  356. (** Send data on a TCP connection. *)
  357. PROCEDURE Send*(CONST data: ARRAY OF CHAR; ofs, len: LONGINT; propagate: BOOLEAN; VAR res: WORD);
  358. VAR buf: SendBuffer; len0: LONGINT;
  359. BEGIN {EXCLUSIVE}
  360. IF StrongChecks THEN Invariant(SELF) END;
  361. ASSERT(ofs+len <= LEN(data)); (* index check *)
  362. LOOP
  363. IF len <= 0 THEN EXIT END;
  364. IF len <= maxseg THEN len0 := len ELSE len0 := maxseg END;
  365. IF ~((state IN {Established, CloseWait}) & (sndspace >= len0)) THEN (* can not send immediately *)
  366. AWAIT(((state IN {Established, CloseWait}) & (sndspace >= len0)) OR ~(state IN {SynSent..CloseWait}));
  367. IF StrongChecks THEN Invariant(SELF) END;
  368. IF ~(state IN {SynSent..CloseWait}) THEN (* connection broken *)
  369. IF error # Ok THEN res := error ELSE res := NotConnected END;
  370. RETURN
  371. END
  372. END;
  373. buf := sndtail;
  374. IF LEN(buf.data^) - (buf.ofs+buf.len) >= len0 THEN (* last buffer has space for data *)
  375. IF SystemMove THEN
  376. SYSTEM.MOVE(ADDRESSOF(data[ofs]), ADDRESSOF(buf.data[buf.ofs+buf.len]), len0)
  377. ELSE
  378. Network.Copy(data, buf.data^, ofs, buf.ofs+buf.len, len0)
  379. END;
  380. INC(buf.len, len0)
  381. ELSE (* last buffer has no space for data *)
  382. buf := buf.next;
  383. IF buf # sndhead THEN (* is free buffer *)
  384. ASSERT((buf.ofs = 0) & (buf.len = 0)); (* buffer must be unused *)
  385. ASSERT(LEN(buf.data^) >= len0) (* index check *)
  386. ELSE
  387. Machine.AtomicInc(NTCPNewBufs);
  388. NEW(buf); NEW(buf.data, MSS * SegsPerBuf);
  389. IF ~NewZeros THEN buf.ofs := 0; END;
  390. buf.next := sndtail.next; sndtail.next := buf;
  391. ASSERT(LEN(buf.data^) >= len0) (* index check *)
  392. END;
  393. IF SystemMove THEN
  394. SYSTEM.MOVE(ADDRESSOF(data[ofs]), ADDRESSOF(buf.data[0]), len0)
  395. ELSE
  396. Network.Copy(data, buf.data^, ofs, 0, len0)
  397. END;
  398. buf.len := len0; sndtail := buf
  399. END;
  400. INC(sndcc, len0); DEC(sndspace, len0);
  401. Output(SELF);
  402. INC(ofs, len0); DEC(len, len0)
  403. END;
  404. IF TraceProtocol THEN
  405. TraceTCP("Send", SELF, empty^, data, 0, ofs, len)
  406. END;
  407. res := Ok
  408. END Send;
  409. (** Receive data on a TCP connection. The data parameter specifies the buffer. The ofs parameters specify the position in the buffer where data should be received (usually 0), and the size parameters specifies how many bytes of data can be received in the buffer. The min parameter specifies the minimum number of bytes to receive before Receive returns and must by <= size. The len parameter returns the number of bytes received, and the res parameter returns 0 if ok, or a non-zero error code otherwise (e.g. if the connection is closed by the communication partner, or by a call of the Close method). *)
  410. PROCEDURE Receive*(VAR data: ARRAY OF CHAR; ofs, size, min: LONGINT; VAR len: LONGINT; VAR res: WORD);
  411. VAR
  412. buf: Network.Buffer;
  413. rlen: LONGINT;
  414. BEGIN {EXCLUSIVE}
  415. IF StrongChecks THEN Invariant(SELF) END;
  416. ASSERT((ofs >= 0) & (ofs+size <= LEN(data)) & (min <= size)); (* parameter consistency check *)
  417. len := 0;
  418. LOOP
  419. WHILE (rcvhead # NIL) & (rcvhead # rcvreasm) & (size > 0) DO
  420. IF rcvhead.nextFragment = NIL THEN
  421. (* read all available data until user buffer is full *)
  422. rlen := MIN(rcvhead.len, size);
  423. IF SystemMove THEN
  424. SYSTEM.MOVE(ADDRESSOF(rcvhead.data[rcvhead.ofs]), ADDRESSOF(data[ofs]), rlen);
  425. ELSE
  426. Network.Copy(rcvhead.data, data, rcvhead.ofs, ofs, rlen);
  427. END;
  428. INC(len, rlen);
  429. INC(ofs, rlen);
  430. DEC(size, rlen);
  431. INC(rcvhead.ofs, rlen);
  432. DEC(rcvhead.len, rlen);
  433. INC(rcvhead.int, rlen);
  434. INC(rcvspace, rlen);
  435. IF rcvhead.len = 0 THEN
  436. (* go to next buffer *)
  437. buf := rcvhead;
  438. rcvhead := rcvhead.next;
  439. IF rcvhead # NIL THEN
  440. rcvhead.prev := NIL;
  441. END;
  442. Network.ReturnBuffer(buf);
  443. Output(SELF); (* enable sending window update segment *)
  444. END;
  445. ELSE
  446. (* rcvhead has fragments *)
  447. (* read all available data until user buffer is full *)
  448. IF rcvheadFragment = NIL THEN
  449. rcvheadFragment := rcvhead;
  450. END;
  451. rlen := MIN(rcvheadFragment.len, size);
  452. IF SystemMove THEN
  453. SYSTEM.MOVE(ADDRESSOF(rcvheadFragment.data[rcvheadFragment.ofs]), ADDRESSOF(data[ofs]), rlen);
  454. ELSE
  455. Network.Copy(rcvheadFragment.data, data, rcvheadFragment.ofs, ofs, rlen);
  456. END;
  457. INC(len, rlen);
  458. INC(ofs, rlen);
  459. DEC(size, rlen);
  460. INC(rcvheadFragment.ofs, rlen);
  461. DEC(rcvheadFragment.len, rlen);
  462. INC(rcvheadFragment.int, rlen);
  463. INC(rcvspace, rlen);
  464. IF rcvheadFragment.len = 0 THEN
  465. IF rcvheadFragment.nextFragment # NIL THEN
  466. (* go to next fragment *)
  467. rcvheadFragment := rcvheadFragment.nextFragment;
  468. ELSE
  469. (* go to next buffer *)
  470. buf := rcvhead;
  471. rcvhead := rcvhead.next;
  472. IF rcvhead # NIL THEN
  473. rcvhead.prev := NIL;
  474. END;
  475. Network.ReturnBuffer(buf);
  476. Output(SELF); (* enable sending window update segment *)
  477. END;
  478. END;
  479. END;
  480. END;
  481. IF size = 0 THEN
  482. (* user buffer full *)
  483. EXIT;
  484. END;
  485. IF len >= min THEN
  486. (* enough was read *)
  487. EXIT;
  488. ELSE
  489. (* await available data or closed connection state *)
  490. AWAIT(((rcvhead # NIL) & (rcvhead # rcvreasm)) OR ~(state IN {SynSent, SynReceived, Established, FinWait1, FinWait2}));
  491. IF StrongChecks THEN Invariant(SELF) END;
  492. IF (rcvhead # NIL) & (rcvhead # rcvreasm) THEN
  493. (* new data available, start again with LOOP *)
  494. ELSE
  495. (* no data available, and no more can arrive, as we've seen the FIN *)
  496. IF error # Ok THEN res := error ELSE res := Streams.EOF (* end of file *) END;
  497. RETURN;
  498. END;
  499. END;
  500. END;
  501. IF StrongChecks THEN Invariant(SELF) END;
  502. IF TraceProtocol THEN
  503. TraceTCP("Receive", SELF, empty^, data, 0, ofs, len)
  504. END;
  505. res := Ok
  506. END Receive;
  507. (** Enable or disable delayed send (Nagle algorithm).
  508. If enabled, the sending of a segment is delayed if it is not filled by one call to Send, in order to be able to be filled
  509. by further calls to Send. This is the default option.
  510. If disabled, a segment is sent immediatly after a call to Send, even if it is not filled. This option is normally chosen
  511. by applications like telnet or VNC client, which send verly little data but shall not be delayed.
  512. *)
  513. PROCEDURE DelaySend*(enable: BOOLEAN);
  514. BEGIN {EXCLUSIVE}
  515. IF enable THEN
  516. EXCL(flags, NoDelay);
  517. ELSE
  518. INCL(flags, NoDelay);
  519. END;
  520. END DelaySend;
  521. (** Enable or disable keep-alive. (default: disabled) *)
  522. PROCEDURE KeepAlive*(enable: BOOLEAN);
  523. BEGIN {EXCLUSIVE}
  524. IF enable THEN
  525. INCL(flags, DoKeepAlive);
  526. ELSE
  527. EXCL(flags, DoKeepAlive);
  528. END;
  529. END KeepAlive;
  530. (** Return number of bytes that may be read without blocking. *)
  531. PROCEDURE Available*(): LONGINT;
  532. VAR
  533. len: LONGINT;
  534. item: Network.Buffer;
  535. fragmentBuffer: Network.Buffer;
  536. reassembledLength: LONGINT;
  537. BEGIN {EXCLUSIVE}
  538. len := 0;
  539. item := rcvhead;
  540. WHILE(item # NIL) & (item # rcvreasm) DO
  541. IF item.nextFragment # NIL THEN
  542. INC(len, item.len);
  543. ELSE
  544. (* fragmented packet *)
  545. fragmentBuffer := item;
  546. reassembledLength := 0;
  547. WHILE fragmentBuffer # NIL DO
  548. INC(len, fragmentBuffer.len);
  549. fragmentBuffer := fragmentBuffer.nextFragment;
  550. END;
  551. END;
  552. item := item.next;
  553. END;
  554. RETURN len;
  555. END Available;
  556. (** Return connection state. *)
  557. PROCEDURE State*(): LONGINT;
  558. BEGIN {EXCLUSIVE}
  559. IF (state IN FinStates) & (rcvhead # NIL) & (rcvhead.len # 0) THEN (* workaround for client errors *)
  560. IF state = CloseWait THEN (* act as if we haven't seen a FIN yet *)
  561. RETURN Established
  562. ELSE
  563. RETURN FinWait1
  564. END
  565. ELSE
  566. RETURN state
  567. END
  568. END State;
  569. (** Wait until the connection state is either in the good or bad set, up to "ms" milliseconds. *)
  570. PROCEDURE AwaitState*(good, bad: SET; ms: LONGINT; VAR res: WORD);
  571. BEGIN {EXCLUSIVE}
  572. IF ~(state IN (good+bad)) THEN
  573. IF ms # -1 THEN
  574. IF timeout = NIL THEN NEW(timeout) END;
  575. timeout.SetTimeout(SELF.HandleTimeout, ms);
  576. END;
  577. EXCL(flags, Timeout);
  578. AWAIT((state IN (good+bad)) OR (Timeout IN flags));
  579. IF ms # -1 THEN timeout.CancelTimeout(); END
  580. END;
  581. IF state IN good THEN
  582. res := Ok
  583. ELSIF state IN bad THEN
  584. res := NotConnected
  585. ELSE
  586. res := TimedOut
  587. END
  588. END AwaitState;
  589. PROCEDURE HandleTimeout;
  590. BEGIN {EXCLUSIVE}
  591. INCL(flags, Timeout)
  592. END HandleTimeout;
  593. (** Close a TCP connection (half-close). *)
  594. PROCEDURE Close*;
  595. BEGIN {EXCLUSIVE}
  596. IF state < Established THEN
  597. CloseConnection(SELF)
  598. ELSIF FALSE (* linger *) THEN
  599. Drop(SELF, 0)
  600. ELSE
  601. UsrClosed(SELF);
  602. IF state # Closed THEN Output(SELF) END
  603. END;
  604. IF TraceProtocol THEN
  605. TraceTCP("Close", SELF, empty^, empty^, 0, 0, 0)
  606. END
  607. END Close;
  608. (** Discard a TCP connection (shutdown). *)
  609. PROCEDURE Discard*;
  610. BEGIN {EXCLUSIVE}
  611. IF state < Established THEN
  612. CloseConnection(SELF)
  613. ELSE
  614. Drop(SELF, ConnectionReset) (* ??? *)
  615. END;
  616. IF TraceProtocol THEN
  617. TraceTCP("Discard", SELF, empty^, empty^, 0, 0, 0)
  618. END
  619. END Discard;
  620. (** Accept a client waiting on a listening connection. Blocks until a client is available or the connection is closed. *)
  621. PROCEDURE Accept*(VAR client: Connection; VAR res: WORD);
  622. BEGIN {EXCLUSIVE}
  623. AWAIT((state # Listen) OR (acceptNext # NIL));
  624. IF acceptNext # NIL THEN
  625. client := acceptNext; acceptNext := acceptNext.acceptNext;
  626. INC(acceptable); res := Ok
  627. ELSE
  628. client := NIL; res := ConnectionRefused
  629. END
  630. END Accept;
  631. (** Return TRUE iff a listening connection has clients waiting to be accepted. *)
  632. PROCEDURE Requested*(): BOOLEAN;
  633. BEGIN {EXCLUSIVE}
  634. RETURN (state = Listen) & (acceptNext # NIL)
  635. END Requested;
  636. (* Process a received segment for the current Connection. *)
  637. PROCEDURE Input(int: IP.Interface; fip: IP.Adr; hdrLen: LONGINT; buffer: Network.Buffer);
  638. VAR
  639. bufferQueued: BOOLEAN; (* was buffer queued by ProcessInput() ? *)
  640. p: Connection;
  641. BEGIN {EXCLUSIVE}
  642. (* to do: move header prediction code here *)
  643. IF StrongChecks THEN Invariant(SELF) END;
  644. bufferQueued := FALSE;
  645. IF AcceptConn IN flags THEN
  646. IF acceptable > 0 THEN
  647. NEW(p); InitConnection(p); (* fig. 28.7 *)
  648. p.int := int;
  649. p.fip := fip;
  650. p.state := Listen; p.parent := SELF;
  651. ProcessInput(p, hdrLen, buffer, TRUE, bufferQueued);
  652. IF p.state = SynReceived THEN (* packet was accepted *)
  653. DEC(acceptable) (* limit number of "temporary" Connections *)
  654. END
  655. ELSE
  656. Machine.AtomicInc(NTCPUnacceptable)
  657. END
  658. ELSE
  659. IF SELF = nilpcb THEN
  660. (* set info for "Respond" *)
  661. SELF.int := int;
  662. SELF.fport := Network.GetNet2(buffer.data, buffer.ofs);
  663. SELF.lport := Network.GetNet2(buffer.data, buffer.ofs+2);
  664. END;
  665. IF SELF.int # int THEN
  666. (* packet must be received by interface attached to this connection *)
  667. Error(WrongInterface, 0, SELF);
  668. ELSE
  669. SELF.fip := fip;
  670. ProcessInput(SELF, hdrLen, buffer, FALSE, bufferQueued);
  671. END;
  672. END;
  673. IF StrongChecks THEN Invariant(SELF) END;
  674. IF ~bufferQueued THEN
  675. Network.ReturnBuffer(buffer);
  676. END;
  677. END Input;
  678. (* Schedule a delayed ack. *)
  679. PROCEDURE DelayedAck;
  680. BEGIN {EXCLUSIVE}
  681. IF StrongChecks THEN Invariant(SELF) END;
  682. IF DelAck IN flags THEN
  683. flags := (flags - {DelAck}) + {AckNow};
  684. Machine.AtomicInc(NTCPDelAck);
  685. Output(SELF)
  686. END
  687. END DelayedAck;
  688. (* Schedule a slow timer event (fig. 25.8). *)
  689. PROCEDURE SlowTimer;
  690. VAR dropit: BOOLEAN;
  691. oldie: LONGINT;
  692. BEGIN {EXCLUSIVE}
  693. oldie := sndnxt;
  694. IF StrongChecks THEN Invariant(SELF) END;
  695. IF Expired(timer[ReXmt]) THEN (* fig. 25.26 *)
  696. INC(rxtshift);
  697. IF rxtshift > MaxRxtShift THEN
  698. rxtshift := MaxRxtShift; Error(TimedOut, 0, SELF); Drop(SELF, TimedOut)
  699. ELSE
  700. Machine.AtomicInc(NTCPReXmtTimer);
  701. RangeSet(rxtcur, (ASH(srtt, -RTTShift) + rttvar) * backoff[rxtshift], rttmin, ReXmtMax);
  702. timer[ReXmt] := rxtcur;
  703. IF rxtshift > MaxRxtShift DIV 4 THEN
  704. (* to do: drop current route *)
  705. INC(rttvar, ASH(srtt, -RTTShift));
  706. srtt := 0
  707. END;
  708. sndnxt := snduna; rtt := 0;
  709. sndcwnd := maxseg; dupacks := 0;
  710. sndssthresh := MAX(MIN(sndwnd, sndcwnd) DIV 2 DIV maxseg, 2) * maxseg;
  711. IF TraceCongestion THEN
  712. KernelLog.String("ST sndssthresh := "); KernelLog.Int(sndssthresh, 1); KernelLog.Ln
  713. END;
  714. Output(SELF)
  715. END
  716. END;
  717. IF Expired(timer[Persist]) THEN (* fig. 25.13 *)
  718. Machine.AtomicInc(NTCPPersistTimer);
  719. IF (rxtshift = MaxRxtShift) & ((idle >= MaxPersistIdle) OR (idle >= (ASH(srtt, -RTTShift) + rttvar) * totbackoff)) THEN
  720. Machine.AtomicInc(NTCPPersistDrop); (* vol. 3 fig. 14.17 *)
  721. Drop(SELF, TimedOut)
  722. ELSE
  723. SetPersist(SELF); INCL(flags, Force); Output(SELF); EXCL(flags, Force)
  724. END
  725. END;
  726. traceflow := 0;
  727. IF Expired(timer[Keep]) THEN (* fig. 25.16 *)
  728. Machine.AtomicInc(NTCPKeepTimer); dropit := FALSE;
  729. IF state >= Established THEN
  730. IF (DoKeepAlive IN flags) & (state <= CloseWait) THEN
  731. IF idle < KeepIdle + MaxIdle THEN
  732. traceflow := 1;
  733. Machine.AtomicInc(NTCPKeepProbe);
  734. Respond(SELF, rcvnxt, snduna-1, {});
  735. timer[Keep] := KeepIntvl
  736. ELSE
  737. traceflow := 2;
  738. dropit := TRUE; Error(TimeOutKeepAlive, 0, SELF)
  739. END
  740. ELSE
  741. traceflow := 3;
  742. timer[Keep] := KeepIdle
  743. END
  744. ELSE
  745. traceflow := 4;
  746. dropit := TRUE; Error(TimeoutEstablished, 0, SELF)
  747. END;
  748. IF dropit THEN Drop(SELF, TimedOut) END
  749. END;
  750. IF Expired(timer[MSL2]) THEN (* fig. 25.10 *)
  751. IF (state # TimeWait) & (idle <= MaxIdle) THEN
  752. timer[MSL2] := KeepIntvl
  753. ELSE
  754. IF state = FinWait2 THEN Machine.AtomicInc(NTCPFinWait2Timer) ELSE Machine.AtomicInc(NTCPTimeWaitTimer) END;
  755. CloseConnection(SELF)
  756. END
  757. END;
  758. INC(idle);
  759. IF rtt # 0 THEN INC(rtt) END;
  760. (* check if interface wasn't closed/removed in meantime *)
  761. IF (int # NIL) & (int.closed) THEN
  762. Drop(SELF, InterfaceClosed);
  763. END;
  764. END SlowTimer;
  765. (* Finalize the Connection object *)
  766. PROCEDURE Finalize;
  767. BEGIN
  768. IF timeout # NIL THEN timeout.Finalize; END;
  769. Discard();
  770. END Finalize;
  771. END Connection;
  772. TYPE
  773. ConnectionHandler* = PROCEDURE {DELEGATE} (p: Connection);
  774. ConnectionPool* = OBJECT
  775. VAR
  776. eport: LONGINT;
  777. table: ARRAY HashTableSize OF Connection;
  778. (* Initialization for internal use only. *)
  779. PROCEDURE &Init*;
  780. VAR i: LONGINT;
  781. BEGIN
  782. FOR i:= 0 TO HashTableSize-1 DO
  783. table[i] := NIL;
  784. END;
  785. eport := MinEphemeralPort;
  786. END Init;
  787. (* Finalize all connections in this pool *)
  788. PROCEDURE Finalize;
  789. VAR i: LONGINT;
  790. BEGIN
  791. FOR i:= 0 TO HashTableSize-1 DO
  792. WHILE table[i] # NIL DO
  793. table[i].Finalize();
  794. END;
  795. END;
  796. END Finalize;
  797. (* Look for the specified Connection. *)
  798. PROCEDURE Lookup(lport, fport: LONGINT; fip: IP.Adr): Connection;
  799. VAR
  800. item: Connection;
  801. BEGIN
  802. item := table[HashPool(lport, fport, fip)];
  803. WHILE (item # NIL) & ((~IP.AdrsEqual(item.fip, fip)) OR (item.fport # fport) OR (item.lport # lport)) DO
  804. item := item.poolNext;
  805. END;
  806. IF item = NIL THEN
  807. RETURN nilpcb;
  808. ELSE
  809. RETURN item;
  810. END;
  811. END Lookup;
  812. (** Enumerate all Connections. Only for tracing, due to concurrent updates data may be stale. *)
  813. PROCEDURE Enumerate*(handle: ConnectionHandler);
  814. VAR
  815. i: LONGINT;
  816. item: Connection;
  817. BEGIN
  818. FOR i:= 0 TO HashTableSize-1 DO
  819. item := table[i];
  820. WHILE item # NIL DO
  821. handle(item);
  822. item := item.poolNext;
  823. END;
  824. END;
  825. END Enumerate;
  826. (* Add the connection p to the pool (attach). Assumes the caller has exclusive access to p.
  827. IF (fport = NilPort) & (fip = IP.NilAdr), a listening connection is assumed.
  828. ELSE an active open is assumed.
  829. IF (lport = NilPort), an ephemeral port is assigned.
  830. *)
  831. PROCEDURE Add(p: Connection; lport, fport: LONGINT; VAR res: WORD);
  832. VAR i, sport: LONGINT;
  833. BEGIN {EXCLUSIVE}
  834. IF ((fport # NilPort) & (IP.IsNilAdr(p.fip))) OR (* workaround for XOR *)
  835. ((fport = NilPort) & (~IP.IsNilAdr(p.fip))) THEN
  836. (* both must be "nil" or both must not be "nil" *)
  837. res := InvalidParameter;
  838. Error(res, 0, p);
  839. RETURN;
  840. END;
  841. IF lport = NilPort THEN
  842. (* find an ephemeral port *)
  843. sport := eport;
  844. LOOP
  845. lport := eport;
  846. INC(eport);
  847. IF eport > MaxEphemeralPort THEN
  848. eport := MinEphemeralPort;
  849. END;
  850. IF Lookup(lport, fport, p.fip) = nilpcb THEN
  851. (* found port that is not in use *)
  852. EXIT;
  853. END;
  854. IF eport = sport THEN
  855. res := AllPortsInUse;
  856. Error(res, 0, p);
  857. RETURN;
  858. END;
  859. END;
  860. ELSE
  861. IF Lookup(lport, fport, p.fip) # nilpcb THEN
  862. res := AddressInUse;
  863. Error(res, 0, p);
  864. RETURN;
  865. END;
  866. END;
  867. p.lport := lport;
  868. p.fport := fport;
  869. (* add to pool *)
  870. i := HashPool(lport, fport, p.fip);
  871. p.poolNext := table[i];
  872. table[i] := p;
  873. res := Ok;
  874. END Add;
  875. (* Remove a Connection from the queue (detach), making its address re-usable. *)
  876. PROCEDURE Remove(p: Connection);
  877. VAR
  878. i: LONGINT;
  879. item: Connection;
  880. BEGIN {EXCLUSIVE}
  881. i := HashPool(p.lport, p.fport, p.fip);
  882. IF table[i] # NIL THEN
  883. IF table[i] = p THEN
  884. (* remove first item *)
  885. table[i] := table[i].poolNext;
  886. RETURN;
  887. ELSE
  888. (* search list for connection *)
  889. item := table[i];
  890. WHILE (item.poolNext # NIL) & (item.poolNext # p) DO
  891. item := item.poolNext;
  892. END;
  893. IF item.poolNext # NIL THEN
  894. (* found - remove *)
  895. item.poolNext := item.poolNext.poolNext;
  896. RETURN;
  897. END;
  898. END;
  899. END;
  900. Error(ConnectionGone, 0, p); (* pcb gone, e.g. Rst received *)
  901. END Remove;
  902. END ConnectionPool;
  903. TYPE
  904. (** Install a procedure to be called when no matching port was found for an incoming connection request.
  905. The buffer MUST NOT be returned by the listener, it is returned automatically by the caller afterwards.
  906. *)
  907. PacketDumpListener* = PROCEDURE (fip: IP.Adr; buffer: Network.Buffer);
  908. VAR
  909. pool*: ConnectionPool; (* pool of all Connections *)
  910. timeSource: Timer; (* global timer *)
  911. issSource: ISS; (* source for ISS numbers *)
  912. lastpcb: Connection; (* cache last used pcb (never NIL). Note that it is possible that a deleted pcb can be ressurrected by a packet arriving for it. As soon as a packet for another connection arrives, the deleted pcb will vanish. *)
  913. nilpcb: Connection; (* never NIL *)
  914. empty: SendData; (* never NIL *)
  915. backoff: ARRAY MaxRxtShift+1 OF LONGINT; (* exponential backoff multipliers *)
  916. totbackoff: LONGINT;
  917. outflags: ARRAY NumStates OF SET; (* output header flags *)
  918. (* TCP counters *)
  919. NTCPError-: ARRAY NumErrors OF LONGINT;
  920. NTCPConnectAttempt-, NTCPPersistTimer-, NTCPFinWait2Timer-, NTCPSendProbe-, NTCPReXmtPack-,
  921. NTCPReXmtByte-, NTCPSendPack-, NTCPSendByte-, NTCPAcks-, NTCPSendCtrl-, NTCPSendUrg-,
  922. NTCPSendWinUp-, NTCPSegsTimed-, NTCPSendTotal-, NTCPKeepTimer-, NTCPKeepProbe-,
  923. NTCPReXmtTimer-, NTCPRcvTotal-, NTCPRcvOptions-, NTCPCacheMiss-, NTCPPredAck-, NTCPAckPack-,
  924. NTCPAckByte-, NTCPPredData-, NTCPRcvPackFast-, NTCPRcvByteFast-, NTCPConnects-, NTCPRcvWinProbe-,
  925. NTCPDrops-, NTCPRcvWinUpd-, NTCPRTTUpdated-, NTCPDelAck-, NTCPConnDrops-, NTCPClosed-, NTCPSplitBuffer-,
  926. NTCPRcvPackSlow-, NTCPRcvByteSlow-, NTCPNewBufs-, NTCPTimeWaitTimer-,
  927. NTCPUnacceptable-, NTCPAccepts-, NTCPPersistDrop-: LONGINT;
  928. trace: BOOLEAN;
  929. packetDumpListener : PacketDumpListener;
  930. (* Trace installer *)
  931. (** Install a procedure to be called when no matching port was found for an incoming connection request.
  932. The buffer doesn't have to be returned by the listener, it is returned automatically by the caller afterwards.
  933. *)
  934. PROCEDURE SetDefaultListener*(pdl : PacketDumpListener);
  935. BEGIN
  936. packetDumpListener := pdl
  937. END SetDefaultListener;
  938. (* --- Utility procedures. *)
  939. PROCEDURE Invariant(p: Connection);
  940. VAR
  941. rcvbuf: Network.Buffer;
  942. sndbuf: SendBuffer;
  943. found: BOOLEAN;
  944. BEGIN
  945. IF StrongChecks & (p # nilpcb) THEN
  946. (* receive buffers *)
  947. rcvbuf := p.rcvhead;
  948. IF rcvbuf # NIL THEN
  949. (* not empty *)
  950. ASSERT((rcvbuf.len > 0) OR (Fin IN rcvbuf.set));
  951. ASSERT(rcvbuf.prev = NIL);
  952. IF rcvbuf.next = NIL THEN
  953. (* the only buffer *)
  954. ASSERT(p.rcvtail = rcvbuf);
  955. ELSE
  956. rcvbuf := rcvbuf.next;
  957. IF rcvbuf.next = NIL THEN
  958. (* 2 buffers *)
  959. ASSERT(rcvbuf.prev.next = rcvbuf);
  960. ASSERT(p.rcvtail = rcvbuf);
  961. ELSE
  962. (* check chain (more than 2 buffers *)
  963. REPEAT
  964. ASSERT(rcvbuf.next.prev = rcvbuf);
  965. ASSERT(rcvbuf.prev.next = rcvbuf);
  966. rcvbuf := rcvbuf.next;
  967. UNTIL rcvbuf.next = NIL;
  968. (* last buffer *)
  969. ASSERT(p.rcvtail = rcvbuf);
  970. END;
  971. END;
  972. END;
  973. (* send buffers *)
  974. sndbuf := p.sndhead; found := FALSE;
  975. LOOP
  976. found := found OR (sndbuf = p.sndtail);
  977. sndbuf := sndbuf.next;
  978. ASSERT(sndbuf # NIL);
  979. IF sndbuf = p.sndhead THEN EXIT END;
  980. ASSERT(~found OR ((sndbuf.ofs = 0) & (sndbuf.len = 0)))
  981. END;
  982. ASSERT(found);
  983. END
  984. END Invariant;
  985. (* Hash function for ConnectionPool *)
  986. PROCEDURE HashPool(lport, fport: LONGINT; fip:IP.Adr): LONGINT;
  987. VAR
  988. i: LONGINT;
  989. hash: LONGINT;
  990. BEGIN
  991. (* hash := (lport + fport + fip) MOD HashTableSize; *)
  992. hash := lport + fport;
  993. CASE fip.usedProtocol OF
  994. IP.IPv4:
  995. INC(hash, fip.ipv4Adr);
  996. |IP.IPv6:
  997. FOR i := 0 TO 15 DO
  998. INC(hash, ORD(fip.ipv6Adr[i]));
  999. END;
  1000. ELSE
  1001. END;
  1002. RETURN hash MOD HashTableSize;
  1003. END HashPool;
  1004. (*
  1005. (* Hash function for ConnectionPool. *)
  1006. PROCEDURE -HashPool(lport, fport: LONGINT; fip: IP.Adr): LONGINT;
  1007. CODE {SYSTEM.i386}
  1008. (* hash := (lport + fport + fip) MOD HashTableSize; *)
  1009. POP ECX
  1010. POP EBX
  1011. POP EAX
  1012. ; Convert IP to host byte order
  1013. XCHG CL, CH
  1014. ROL ECX, 16
  1015. XCHG CL, CH
  1016. ; Calculate sum
  1017. ADD EAX, EBX
  1018. ADD EAX, ECX
  1019. ; MOD operation
  1020. MOV EBX, HashTableSize
  1021. XOR EDX, EDX
  1022. DIV EBX
  1023. MOV EAX, EDX
  1024. END HashPool;
  1025. *)
  1026. (* Set x to val, but keep it between min and max. *)
  1027. PROCEDURE RangeSet(VAR x: LONGINT; val, min, max: LONGINT);
  1028. BEGIN
  1029. IF val < min THEN x := min
  1030. ELSIF val > max THEN x := max
  1031. ELSE x := val
  1032. END
  1033. END RangeSet;
  1034. PROCEDURE WriteTime(t: LONGINT);
  1035. VAR s: ARRAY 8 OF CHAR;
  1036. BEGIN
  1037. KernelLog.Int(t DIV Kernel.second, 1);
  1038. s[0] := "."; t := (t MOD Kernel.second)*1000 DIV Kernel.second;
  1039. s[1] := CHR(48+t DIV 100 MOD 10); s[2] := CHR(48+t DIV 10 MOD 10);
  1040. s[3] := CHR(48+t MOD 10); s[4] := 0X;
  1041. KernelLog.String(s)
  1042. END WriteTime;
  1043. (* TCP error function. *)
  1044. PROCEDURE Error(err: WORD; n: LONGINT; p: Connection);
  1045. BEGIN
  1046. IF trace THEN
  1047. KernelLog.Enter; KernelLog.String("TCP: "); WriteTime(Kernel.GetTicks());
  1048. KernelLog.String(" result "); KernelLog.Int(err, 1);
  1049. KernelLog.Char(" "); KernelLog.Int(n, 1); KernelLog.Exit
  1050. END;
  1051. IF TraceError & (p # NIL) THEN TraceTCP("", p, empty^, empty^, 0, 0, 0) END;
  1052. Machine.AtomicInc(NTCPError[0]); Machine.AtomicInc(NTCPError[err-MinError])
  1053. END Error;
  1054. (* Calculate and store new value for persist timer. *)
  1055. PROCEDURE SetPersist(p: Connection);
  1056. BEGIN
  1057. ASSERT(p.timer[ReXmt] = 0);
  1058. RangeSet(p.timer[Persist], (p.srtt DIV 4 + p.rttvar) DIV 2 * backoff[p.rxtshift], PersMin, PersMax);
  1059. IF p.rxtshift < MaxRxtShift THEN INC(p.rxtshift) END
  1060. END SetPersist;
  1061. (* Trace Connection fields. *)
  1062. PROCEDURE TraceConnection(p: Connection);
  1063. VAR i: LONGINT;
  1064. BEGIN
  1065. IF Trace THEN
  1066. KernelLog.String(" state=");
  1067. CASE p.state OF
  1068. Closed: KernelLog.String("Closed")
  1069. |Listen: KernelLog.String("Listen")
  1070. |SynSent: KernelLog.String("SynSent")
  1071. |SynReceived: KernelLog.String("SynReceived")
  1072. |Established: KernelLog.String("Established")
  1073. |CloseWait: KernelLog.String("CloseWait")
  1074. |FinWait1: KernelLog.String("FinWait1")
  1075. |Closing: KernelLog.String("Closing")
  1076. |LastAck: KernelLog.String("LastAck")
  1077. |FinWait2: KernelLog.String("FinWait2")
  1078. |TimeWait: KernelLog.String("TimeWait")
  1079. END;
  1080. KernelLog.String(" maxseg="); KernelLog.Int(p.maxseg, 1);
  1081. KernelLog.String(" flags={");
  1082. IF AckNow IN p.flags THEN KernelLog.String(" AckNow") END;
  1083. IF DelAck IN p.flags THEN KernelLog.String(" DelAck") END;
  1084. IF NoDelay IN p.flags THEN KernelLog.String(" NoDelay") END;
  1085. IF SentFin IN p.flags THEN KernelLog.String(" SentFin") END;
  1086. IF Force IN p.flags THEN KernelLog.String(" Force") END;
  1087. IF RcvdScale IN p.flags THEN KernelLog.String(" RcvdScale") END;
  1088. IF RcvdTstmp IN p.flags THEN KernelLog.String(" RcvdTstmp") END;
  1089. IF ReqScale IN p.flags THEN KernelLog.String(" ReqScale") END;
  1090. IF ReqTstmp IN p.flags THEN KernelLog.String(" ReqTstmp") END;
  1091. IF DoKeepAlive IN p.flags THEN KernelLog.String(" DoKeepAlive") END;
  1092. IF AcceptConn IN p.flags THEN KernelLog.String(" AcceptConn") END;
  1093. FOR i := 11 TO 31 DO
  1094. IF i IN p.flags THEN KernelLog.Char(" "); KernelLog.Int(i, 1) END
  1095. END;
  1096. KernelLog.String(" } error="); KernelLog.Int(p.error, 1);
  1097. KernelLog.Ln;
  1098. KernelLog.String(" iss="); KernelLog.Int(p.iss, 1);
  1099. KernelLog.String(" snduna="); KernelLog.Int(p.snduna-p.iss, 1);
  1100. KernelLog.String(" sndnxt="); KernelLog.Int(p.sndnxt-p.iss, 1);
  1101. KernelLog.String(" sndmax="); KernelLog.Int(p.sndmax-p.iss, 1);
  1102. KernelLog.String(" sndup="); KernelLog.Int(p.sndup-p.iss, 1);
  1103. KernelLog.String(" sndwl2="); KernelLog.Int(p.sndwl2-p.iss, 1);
  1104. KernelLog.String(" rtseq="); KernelLog.Int(p.rtseq-p.iss, 1);
  1105. KernelLog.Ln;
  1106. KernelLog.String(" sndwnd="); KernelLog.Int(p.sndwnd, 1);
  1107. KernelLog.String(" sndcwnd="); KernelLog.Int(p.sndcwnd, 1);
  1108. KernelLog.String(" sndcc="); KernelLog.Int(p.sndcc, 1);
  1109. KernelLog.String(" sndspace="); KernelLog.Int(p.sndspace, 1);
  1110. KernelLog.String(" sndssthresh="); KernelLog.Int(p.sndssthresh, 1);
  1111. KernelLog.Ln;
  1112. KernelLog.String(" irs="); KernelLog.Int(p.irs, 1);
  1113. KernelLog.String(" rcvnxt="); KernelLog.Int(p.rcvnxt-p.irs, 1);
  1114. KernelLog.String(" rcvup="); KernelLog.Int(p.rcvup-p.irs, 1);
  1115. KernelLog.String(" sndwl1="); KernelLog.Int(p.sndwl1-p.irs, 1);
  1116. KernelLog.String(" rcvadv="); KernelLog.Int(p.rcvadv-p.irs, 1);
  1117. KernelLog.String(" lastacksent="); KernelLog.Int(p.lastacksent-p.irs, 1);
  1118. KernelLog.Ln;
  1119. KernelLog.String(" rcvwnd="); KernelLog.Int(p.rcvwnd, 1);
  1120. KernelLog.String(" rcvspace="); KernelLog.Int(p.rcvspace, 1);
  1121. KernelLog.String(" rcvhiwat="); KernelLog.Int(p.rcvhiwat, 1);
  1122. KernelLog.String(" idle="); KernelLog.Int(p.idle, 1);
  1123. KernelLog.String(" rtt="); KernelLog.Int(p.rtt, 1);
  1124. KernelLog.String(" srtt="); KernelLog.Int(p.srtt, 1);
  1125. KernelLog.Ln;
  1126. KernelLog.String(" rttvar="); KernelLog.Int(p.rttvar, 1);
  1127. KernelLog.String(" rttmin="); KernelLog.Int(p.rttmin, 1);
  1128. KernelLog.String(" maxsndwnd="); KernelLog.Int(p.maxsndwnd, 1);
  1129. KernelLog.String(" sndscale="); KernelLog.Int(p.sndscale, 1);
  1130. KernelLog.String(" rcvscale="); KernelLog.Int(p.rcvscale, 1);
  1131. KernelLog.String(" requestrscale="); KernelLog.Int(p.requestrscale, 1);
  1132. KernelLog.Ln;
  1133. KernelLog.String(" requestedsscale="); KernelLog.Int(p.requestedsscale, 1);
  1134. KernelLog.String(" tsrecent="); KernelLog.Int(p.tsrecent, 1);
  1135. KernelLog.String(" tsrecentage="); KernelLog.Int(p.tsrecentage, 1);
  1136. KernelLog.String(" rxtshift="); KernelLog.Int(p.rxtshift, 1);
  1137. KernelLog.String(" rxtcur="); KernelLog.Int(p.rxtcur, 1);
  1138. KernelLog.String(" dupacks="); KernelLog.Int(p.dupacks, 1);
  1139. KernelLog.Ln; KernelLog.Ln
  1140. END
  1141. END TraceConnection;
  1142. (* Trace the protocol. *)
  1143. PROCEDURE TraceTCP(msg: ARRAY OF CHAR; p: Connection; CONST hdr, data: ARRAY OF CHAR; hlen, ofs, len: LONGINT);
  1144. BEGIN
  1145. IF Trace THEN
  1146. WriteTime(Kernel.GetTicks()); KernelLog.Char(" ");
  1147. KernelLog.String(msg); TraceConnection(p);
  1148. IF TracePacket THEN
  1149. KernelLog.Memory(ADDRESSOF(hdr[0]), hlen);
  1150. KernelLog.Memory(ADDRESSOF(data[ofs]), len)
  1151. END
  1152. END
  1153. END TraceTCP;
  1154. (* Output a TCP segment. *)
  1155. PROCEDURE Output(p: Connection);
  1156. VAR
  1157. idle, sendalot: BOOLEAN;
  1158. optLen, off, sum, win, len, adv, x, startseq, left: LONGINT;
  1159. pf: SET; buf: SendBuffer; data: SendData;
  1160. pseudoHdr: ARRAY MaxPseudoHdrLen OF CHAR;
  1161. hdr: ARRAY MaxTCPHdrLen OF CHAR;
  1162. pseudoHdrLen: LONGINT;
  1163. BEGIN
  1164. idle := (p.sndmax = p.snduna);
  1165. IF idle & (p.idle >= p.rxtcur) THEN p.sndcwnd := p.maxseg END;
  1166. REPEAT
  1167. sendalot := FALSE;
  1168. LOOP (* look for reason to send a segment (not a real loop, just a goto-substitute) *)
  1169. off := p.sndnxt - p.snduna; (* the first off bytes from the buffer have been sent and are waiting for Ack *)
  1170. win := p.sndwnd; (* minimum of window advertised by receiver and the congestion window *)
  1171. IF HandleCongestion & (p.sndcwnd < win) THEN win := p.sndcwnd END;
  1172. pf := outflags[p.state];
  1173. IF Force IN p.flags THEN
  1174. IF win = 0 THEN
  1175. IF off < p.sndcc THEN EXCL(pf, Fin) END;
  1176. win := 1
  1177. ELSE
  1178. p.timer[Persist] := 0; p.rxtshift := 0
  1179. END
  1180. END;
  1181. len := p.sndcc; (* minimum of bytes in send buffer and win *)
  1182. IF win < len THEN len := win END;
  1183. DEC(len, off); (* off bytes have already been sent and are waiting for Ack *)
  1184. IF len < 0 THEN
  1185. len := 0;
  1186. IF win = 0 THEN p.timer[ReXmt] := 0; p.sndnxt := p.snduna END;
  1187. END;
  1188. IF len > p.maxseg THEN len := p.maxseg; sendalot := TRUE END;
  1189. IF (p.sndnxt + len) - (p.snduna + p.sndcc) < 0 THEN EXCL(pf, Fin) END; (* not emptying send buffer *)
  1190. win := p.rcvspace; (* now win is receive window advertised *)
  1191. IF len # 0 THEN
  1192. IF len = p.maxseg THEN EXIT END;
  1193. IF (idle OR (NoDelay IN p.flags)) & (len + off >= p.sndcc) THEN EXIT END;
  1194. IF Force IN p.flags THEN EXIT END;
  1195. IF len >= p.maxsndwnd DIV 2 THEN EXIT END;
  1196. IF p.sndnxt - p.sndmax < 0 THEN EXIT END
  1197. END;
  1198. IF win > 0 THEN
  1199. adv := ASH(MaxWin, p.rcvscale);
  1200. IF win < adv THEN adv := win END;
  1201. DEC(adv, p.rcvadv - p.rcvnxt);
  1202. IF adv >= 2*p.maxseg THEN EXIT END;
  1203. IF 2*adv >= p.rcvhiwat THEN EXIT END
  1204. END;
  1205. IF AckNow IN p.flags THEN EXIT END;
  1206. IF pf * {Syn,Rst} # {} THEN EXIT END;
  1207. IF p.sndup - p.snduna > 0 THEN EXIT END;
  1208. IF (Fin IN pf) & (~(SentFin IN p.flags) OR (p.sndnxt = p.snduna)) THEN EXIT END;
  1209. IF (p.sndcc # 0) & (p.timer[ReXmt] = 0) & (p.timer[Persist] = 0) THEN
  1210. p.rxtshift := 0; SetPersist(p)
  1211. END;
  1212. RETURN (* 0 *) (* no reason to send a segment *)
  1213. END; (* LOOP *)
  1214. (* form output segment *)
  1215. optLen := 0;
  1216. IF Syn IN pf THEN
  1217. p.sndnxt := p.iss;
  1218. IF GenOptions THEN
  1219. (* generate MSS option *)
  1220. hdr[MinTCPHdrLen+optLen] := 2X; (* MSS option *)
  1221. hdr[MinTCPHdrLen+optLen+1] := 4X; (* option length *)
  1222. Network.PutNet2(hdr, MinTCPHdrLen+optLen+2, p.int.dev.mtu-120); (* MSS = dev.mtu-120 *)
  1223. INC(optLen, 4);
  1224. (* generate window scale option *)
  1225. IF ((ReqScale IN p.flags) & (~(Ack IN pf) OR (RcvdScale IN p.flags))) THEN
  1226. hdr[MinTCPHdrLen+optLen] := 1X; (* NOP *)
  1227. hdr[MinTCPHdrLen+optLen+1] := 3X; (* window scale option *)
  1228. hdr[MinTCPHdrLen+optLen+2] := 3X; (* option length *)
  1229. hdr[MinTCPHdrLen+optLen+3] := CHR(p.requestrscale); (* window scale *)
  1230. INC(optLen, 4);
  1231. END;
  1232. END;
  1233. END;
  1234. (* generate timestamp option *)
  1235. IF GenOptions & DoRFC1323 & (ReqTstmp IN p.flags) & ~(Rst IN pf) & ((pf * {Syn, Ack} = {Syn}) OR (RcvdTstmp IN p.flags)) THEN
  1236. hdr[MinTCPHdrLen+optLen] := 1X; (* NOP *)
  1237. hdr[MinTCPHdrLen+optLen+1] := 1X; (* NOP *)
  1238. hdr[MinTCPHdrLen+optLen+2] := 8X; (* timestamp option *)
  1239. hdr[MinTCPHdrLen+optLen+3] := 0AX; (* option length *)
  1240. Network.PutNet4(hdr, MinTCPHdrLen+optLen+4, timeSource.now);
  1241. Network.PutNet4(hdr, MinTCPHdrLen+optLen+8, p.tsrecent);
  1242. INC(optLen, 12);
  1243. END;
  1244. (* This doesn't work if Fin was set before !! (bug in TCP/IP Illustrated Vol. 2, p. 873, fig. 26.24)
  1245. Solved by setting MSS = 536 - 12 (size of timestamp option) and commenting this out.
  1246. (mvt, 28.02.2004)
  1247. IF len > p.maxseg - optLen THEN
  1248. len := p.maxseg - optLen;
  1249. sendalot := TRUE;
  1250. END;
  1251. *)
  1252. IF len # 0 THEN
  1253. IF (Force IN p.flags) & (len = 1) THEN Machine.AtomicInc(NTCPSendProbe)
  1254. ELSIF p.sndnxt - p.sndmax < 0 THEN Machine.AtomicInc(NTCPReXmtPack); Machine.AtomicAdd(NTCPReXmtByte, len)
  1255. ELSE Machine.AtomicInc(NTCPSendPack); Machine.AtomicAdd(NTCPSendByte, len)
  1256. END;
  1257. IF off + len = p.sndcc THEN INCL(pf, Psh) END;
  1258. (* data to send is in buffer[off..off+len-1] *)
  1259. buf := p.sndhead; WHILE off >= buf.len DO DEC(off, buf.len); buf := buf.next END;
  1260. IF off+len <= buf.len THEN (* all data is this buffer *)
  1261. data := buf.data; INC(off, buf.ofs)
  1262. ELSE (* data is spread over more buffers *)
  1263. Machine.AtomicInc(NTCPSplitBuffer);
  1264. data := p.sndcontig;
  1265. ASSERT(len <= LEN(data^));
  1266. ASSERT(buf.len-off <= len); (* index check *)
  1267. IF SystemMove THEN
  1268. SYSTEM.MOVE(ADDRESSOF(buf.data[off+buf.ofs]), ADDRESSOF(data[0]), buf.len-off)
  1269. ELSE
  1270. Network.Copy(buf.data^, data^, off+buf.ofs, 0, buf.len-off)
  1271. END;
  1272. off := buf.len-off; left := len-off;
  1273. WHILE left # 0 DO
  1274. buf := buf.next; IF left <= buf.len THEN x := left ELSE x := buf.len END;
  1275. ASSERT(off+x <= len); (* index check *)
  1276. IF SystemMove THEN
  1277. SYSTEM.MOVE(ADDRESSOF(buf.data[buf.ofs]), ADDRESSOF(data[off]), x)
  1278. ELSE
  1279. Network.Copy(buf.data^, data^, buf.ofs, off, x)
  1280. END;
  1281. INC(off, x); DEC(left, x)
  1282. END;
  1283. off := 0
  1284. END
  1285. ELSE
  1286. IF AckNow IN p.flags THEN Machine.AtomicInc(NTCPAcks)
  1287. ELSIF pf * {Syn,Fin,Rst} # {} THEN Machine.AtomicInc(NTCPSendCtrl)
  1288. ELSIF p.sndup - p.snduna > 0 THEN Machine.AtomicInc(NTCPSendUrg)
  1289. ELSE Machine.AtomicInc(NTCPSendWinUp)
  1290. END;
  1291. data := empty; off := 0
  1292. END;
  1293. IF (Fin IN pf) & (SentFin IN p.flags) & (p.sndnxt = p.sndmax) THEN DEC(p.sndnxt) END;
  1294. IF (len # 0) OR (pf * {Syn,Fin} # {}) OR (p.timer[Persist] # 0) THEN
  1295. Network.PutNet4(hdr, 4, p.sndnxt) (* sequence number *)
  1296. ELSE
  1297. Network.PutNet4(hdr, 4, p.sndmax)
  1298. END;
  1299. Network.PutNet4(hdr, 8, p.rcvnxt); (* acknowledgement number *)
  1300. IF (win < p.rcvhiwat DIV 4) & (win < p.maxseg) THEN win := 0 END;
  1301. IF win > ASH(MaxWin, p.rcvscale) THEN win := ASH(MaxWin, p.rcvscale) END;
  1302. IF win < p.rcvadv - p.rcvnxt THEN win := p.rcvadv - p.rcvnxt END;
  1303. Network.PutNet2(hdr, 14, ASH(win, -p.rcvscale));
  1304. IF p.sndup - p.sndnxt > 0 THEN
  1305. x := p.sndup - p.sndnxt;
  1306. IF x > 65535 THEN x := 65535 END;
  1307. Network.PutNet2(hdr, 18, x);
  1308. INCL(pf, Urg)
  1309. ELSE
  1310. p.sndup := p.snduna
  1311. END;
  1312. hdr[13] := CHR(SHORT(SHORT(SYSTEM.VAL(LONGINT, pf))));
  1313. (* set rest of TCP header *)
  1314. Network.PutNet2(hdr, 0, p.lport);
  1315. Network.PutNet2(hdr, 2, p.fport);
  1316. hdr[12] := CHR((MinTCPHdrLen+optLen) DIV 4*10H);
  1317. Network.Put2(hdr, 16, 0); (* checksum := 0; *)
  1318. IF ~(Network.ChecksumTCP IN p.int.dev.calcChecksum) THEN
  1319. (* set pseudo header *)
  1320. pseudoHdrLen := p.int.WritePseudoHeader(pseudoHdr, p.int.localAdr, p.fip, IPTypeTCP, MinTCPHdrLen+optLen+len);
  1321. sum := IP.Checksum1(pseudoHdr, 0, pseudoHdrLen, 0);
  1322. sum := IP.Checksum1(hdr, 0, MinTCPHdrLen+optLen, sum);
  1323. sum := IP.Checksum2(data^, off, len, sum);
  1324. Network.Put2(hdr, 16, sum); (* checksum := sum *)
  1325. END;
  1326. IF ~(Force IN p.flags) OR (p.timer[Persist] = 0) THEN
  1327. startseq := p.sndnxt;
  1328. IF pf * {Syn,Fin} # {} THEN
  1329. IF Syn IN pf THEN INC(p.sndnxt) END;
  1330. IF Fin IN pf THEN INC(p.sndnxt); INCL(p.flags, SentFin) END
  1331. END;
  1332. INC(p.sndnxt, len);
  1333. IF p.sndnxt - p.sndmax > 0 THEN
  1334. p.sndmax := p.sndnxt;
  1335. IF p.rtt = 0 THEN p.rtt := 1; p.rtseq := startseq; Machine.AtomicInc(NTCPSegsTimed) END
  1336. END;
  1337. IF (p.timer[ReXmt] = 0) & (p.sndnxt # p.snduna) THEN
  1338. p.timer[ReXmt] := p.rxtcur;
  1339. IF p.timer[Persist] # 0 THEN p.timer[Persist] := 0; p.rxtshift := 0 END
  1340. END
  1341. ELSIF (p.sndnxt + len) - p.sndmax > 0 THEN
  1342. p.sndmax := p.sndnxt + len
  1343. ELSE (* skip *)
  1344. END;
  1345. IF TraceProtocol THEN
  1346. TraceTCP("Output", p, hdr, data^, MinTCPHdrLen+optLen, off, len)
  1347. END;
  1348. (* Send packet *)
  1349. p.int.Send(IPTypeTCP, p.fip, hdr, data^, MinTCPHdrLen+optLen, off, len, IP.MaxTTL);
  1350. (* old code:
  1351. IP.IPOutput(IP.default, p.hdr, data^, IP.MinIPHdrLen+MinTCPHdrLen, off, len);
  1352. IF FALSE THEN (* error in IPOutput *)
  1353. IF FALSE THEN (* out of buffers *)
  1354. p.sndcwnd := p.maxseg; (* close congestion window *)
  1355. RETURN (* 0 *)
  1356. END;
  1357. IF FALSE & (p.state >= SynReceived) THEN (* host unreachable or network down *)
  1358. (*p.softerror := error;*) RETURN (* 0 *)
  1359. END;
  1360. RETURN (* error *)
  1361. END;
  1362. *)
  1363. Machine.AtomicInc(NTCPSendTotal);
  1364. IF (win > 0) & ((p.rcvnxt + win) - p.rcvadv > 0) THEN p.rcvadv := p.rcvnxt + win END;
  1365. p.lastacksent := p.rcvnxt;
  1366. p.flags := p.flags - {AckNow,DelAck}
  1367. UNTIL ~sendalot;
  1368. (* RETURN 0 *)
  1369. END Output;
  1370. (* Special output function for Rst and KeepAlive packets (fig. 26.34). *)
  1371. PROCEDURE Respond(p: Connection; ack, seq: LONGINT; rf: SET);
  1372. VAR
  1373. win, sum: LONGINT;
  1374. pseudoHdr: ARRAY MaxPseudoHdrLen OF CHAR;
  1375. hdr: ARRAY MaxTCPHdrLen OF CHAR;
  1376. pseudoHdrLen: LONGINT;
  1377. BEGIN
  1378. win := ASH(p.rcvspace, -p.rcvscale); (* zero in nilpcb *)
  1379. IF rf = {} THEN
  1380. (* keepalive probe *)
  1381. INCL(rf, Ack)
  1382. ELSE
  1383. (* Rst segment *)
  1384. END;
  1385. (* set TCP header *)
  1386. Network.PutNet2(hdr, 0, p.lport);
  1387. Network.PutNet2(hdr, 2, p.fport);
  1388. Network.PutNet4(hdr, 4, seq);
  1389. Network.PutNet4(hdr, 8, ack);
  1390. hdr[12] := CHR(MinTCPHdrLen DIV 4*10H);
  1391. hdr[13] := CHR(SHORT(SHORT(SYSTEM.VAL(LONGINT, rf))));
  1392. Network.PutNet2(hdr, 14, win);
  1393. Network.Put2(hdr, 16, 0); (* checksum := 0 *)
  1394. Network.Put2(hdr, 18, 0); (* urgent pointer := 0 *)
  1395. IF ~(Network.ChecksumTCP IN p.int.dev.calcChecksum) THEN
  1396. (* set pseudo header *)
  1397. pseudoHdrLen := p.int.WritePseudoHeader(pseudoHdr, p.int.localAdr, p.fip, IPTypeTCP, MinTCPHdrLen);
  1398. sum := IP.Checksum1(pseudoHdr, 0, pseudoHdrLen, 0);
  1399. sum := IP.Checksum2(hdr, 0, MinTCPHdrLen, sum);
  1400. Network.Put2(hdr, 16, sum); (* checksum := sum; *)
  1401. END;
  1402. p.int.Send(IPTypeTCP, p.fip, hdr, hdr, MinTCPHdrLen, 0, 0, IP.MaxTTL);
  1403. END Respond;
  1404. (* Cancel all timers. *)
  1405. PROCEDURE CancelTimers(p: Connection);
  1406. VAR i: LONGINT;
  1407. BEGIN
  1408. FOR i := 0 TO NumTimers-1 DO p.timer[i] := 0 END
  1409. END CancelTimers;
  1410. (* Apply new RTT measurement to smoothed estimators. *)
  1411. (*
  1412. PROCEDURE XmitTimer(p: Connection; rtt: LONGINT);
  1413. VAR delta: LONGINT;
  1414. BEGIN
  1415. Machine.AtomicInc(NTCPRTTUpdated);
  1416. IF p.srtt # 0 THEN
  1417. delta := rtt - 1 - ASH(p.srtt, -RTTShift);
  1418. INC(p.srtt, delta);
  1419. IF p.srtt <= 0 THEN p.srtt := 1 END;
  1420. IF delta < 0 THEN delta := -delta END;
  1421. DEC(delta, ASH(p.rttvar, -RTTVarShift));
  1422. INC(p.rttvar, delta);
  1423. IF p.rttvar <= 0 THEN p.rttvar := 1 END
  1424. ELSE
  1425. p.srtt := ASH(rtt, RTTShift); p.rttvar := ASH(rtt, RTTVarShift-1)
  1426. END;
  1427. p.rtt := 0; p.rxtshift := 0;
  1428. RangeSet(p.rxtcur, ASH(p.srtt, -RTTShift) + p.rttvar, p.rttmin, ReXmtMax);
  1429. (*p.softerror := 0*)
  1430. END XmitTimer;
  1431. *)
  1432. PROCEDURE XmitTimer(p: Connection; rtt: LONGINT);
  1433. (*
  1434. m -> rtt
  1435. sa -> p.srtt
  1436. sd -> p.rttvar
  1437. *)
  1438. VAR delta: LONGINT;
  1439. BEGIN
  1440. Machine.AtomicInc(NTCPRTTUpdated);
  1441. IF p.srtt # 0 THEN
  1442. (* delta := (rtt*4) - ASH(p.srtt, -RTTShift);*)
  1443. delta := rtt - 1 - ASH(p.srtt, -RTTShift);
  1444. INC(p.srtt, delta);
  1445. IF p.srtt <= 0 THEN p.srtt := 1 END;
  1446. IF delta < 0 THEN delta := -delta END;
  1447. DEC(delta, ASH(p.rttvar, -RTTVarShift));
  1448. INC(p.rttvar, delta);
  1449. IF p.rttvar <= 0 THEN p.rttvar := 1 END
  1450. ELSE
  1451. p.srtt := ASH(rtt, RTTShift); p.rttvar := ASH(rtt, RTTVarShift-1)
  1452. END;
  1453. p.rtt := 0; p.rxtshift := 0;
  1454. (* p.rxtcur:=((p.srtt DIV 8)+ p.rttvar) DIV 4;
  1455. IF p.rxtcur < (rtt+2) THEN p.rxtcur:=rtt; END;*)
  1456. RangeSet(p.rxtcur, ASH(p.srtt, -RTTShift) + p.rttvar, MinTime, ReXmtMax);
  1457. (*p.softerror := 0*)
  1458. END XmitTimer;
  1459. (* Drop len bytes from the front of the send buffer. *)
  1460. PROCEDURE SbDrop(p: Connection; len: LONGINT);
  1461. VAR buf: SendBuffer;
  1462. BEGIN
  1463. DEC(p.sndcc, len); INC(p.sndspace, len);
  1464. buf := p.sndhead;
  1465. LOOP
  1466. IF buf.len > len THEN (* part of buffer will remain *)
  1467. INC(buf.ofs, len); DEC(buf.len, len); (* ignore buf.seq (later: why?) *)
  1468. EXIT
  1469. END;
  1470. DEC(len, buf.len);
  1471. buf.ofs := 0; buf.len := 0; (* make buffer ready for re-use *)
  1472. IF buf # p.sndtail THEN buf := buf.next END;
  1473. IF len = 0 THEN EXIT END
  1474. END;
  1475. p.sndhead := buf
  1476. END SbDrop;
  1477. (* Close a connection (fig. 27.4-6). *)
  1478. PROCEDURE CloseConnection(p: Connection);
  1479. VAR
  1480. buf: Network.Buffer;
  1481. BEGIN
  1482. IF FALSE (* we sent enough data to update rtt in route *) THEN
  1483. END;
  1484. pool.Remove(p);
  1485. (* can not remove from parent's accept list, because of possible deadlock *)
  1486. IF p = lastpcb THEN lastpcb := nilpcb END; (* only for GC -- race does not matter *)
  1487. (* can not clear any pcb pointer fields, because Lookup is non-exclusive *)
  1488. (*SoIsDisconnected(p);*)
  1489. p.state := Closed;
  1490. (* return the buffers still in receive queue *)
  1491. p.rcvreasm := NIL;
  1492. p.rcvtail := NIL;
  1493. WHILE p.rcvhead # NIL DO
  1494. buf := p.rcvhead;
  1495. p.rcvhead := p.rcvhead.next;
  1496. Network.ReturnBuffer(buf);
  1497. END;
  1498. Machine.AtomicInc(NTCPClosed);
  1499. END CloseConnection;
  1500. (* Drop a connection (fig. 27.2). *)
  1501. PROCEDURE Drop(p: Connection; err: LONGINT);
  1502. BEGIN
  1503. IF p.state >= SynReceived THEN
  1504. p.state := Closed; Output(p); Machine.AtomicInc(NTCPDrops)
  1505. ELSE
  1506. Machine.AtomicInc(NTCPConnDrops)
  1507. END;
  1508. (* IF (err = TimedOut) & (p.softerror # Ok) THEN err := p.softerror END; *)
  1509. p.error := err; CloseConnection(p)
  1510. END Drop;
  1511. PROCEDURE SetMSS(p: Connection; mss: LONGINT);
  1512. (* Error(NIYMSS, 0, p); *)
  1513. (* Processing of received MSS option not necessary as long as sending is always done with MSS=536. *)
  1514. END SetMSS;
  1515. (* Process a received TCP segment. *)
  1516. PROCEDURE Input(int: IP.Interface; type: LONGINT; fip, lip: IP.Adr; buffer: Network.Buffer);
  1517. VAR
  1518. lport, fport, hdrLen: LONGINT;
  1519. p: Connection;
  1520. BEGIN
  1521. ASSERT(type = IPTypeTCP);
  1522. Machine.AtomicInc(NTCPRcvTotal);
  1523. IF IP.AdrsEqual(int.localAdr, lip) THEN
  1524. IF buffer.len >= MinTCPHdrLen THEN
  1525. hdrLen := LONG(ORD(buffer.data[buffer.ofs+12])) DIV 10H * 4;
  1526. IF (hdrLen < MinTCPHdrLen) OR (hdrLen > buffer.len) THEN
  1527. (* bad header length *)
  1528. Error(BadHeaderLength, hdrLen, NIL);
  1529. (*GotoDrop;*)
  1530. ELSE
  1531. (* findpcb *)
  1532. p := lastpcb;
  1533. fport := Network.GetNet2(buffer.data, buffer.ofs);
  1534. lport := Network.GetNet2(buffer.data, buffer.ofs+2);
  1535. IF (p = nilpcb) OR (~IP.AdrsEqual(p.fip, fip)) OR (p.lport # lport) OR (p.fport # fport) THEN
  1536. p := pool.Lookup(lport, fport, fip);
  1537. IF p = nilpcb THEN
  1538. (* look for listening connection *)
  1539. p := pool.Lookup(lport, NilPort, IP.NilAdr);
  1540. END;
  1541. lastpcb := p;
  1542. Machine.AtomicInc(NTCPCacheMiss);
  1543. END;
  1544. p.Input(int, fip, hdrLen, buffer);
  1545. RETURN; (* w/o returning buffer *)
  1546. END;
  1547. ELSE
  1548. Error(SegmentTooSmall, buffer.len, NIL);
  1549. END;
  1550. ELSE
  1551. Error(BroadcastReceived, buffer.len, NIL);
  1552. END;
  1553. Network.ReturnBuffer(buffer);
  1554. END Input;
  1555. (* Process a received TCP segment for the specified Connection. *)
  1556. PROCEDURE ProcessInput(p: Connection; hdrLen: LONGINT; buffer: Network.Buffer; drop: BOOLEAN; VAR bufferQueued: BOOLEAN);
  1557. CONST
  1558. Options = 0; TSPresent = 1; NeedOutput = 2;
  1559. VAR
  1560. win, sum, urp, seq, ack, tsval, tsecr, acked, discard, tlen, optLen: LONGINT;
  1561. pseudoHdr: ARRAY MaxPseudoHdrLen OF CHAR;
  1562. pseudoHdrLen: LONGINT;
  1563. pf, lf: SET;
  1564. reassembledLength: LONGINT;
  1565. fragmentBuffer: Network.Buffer;
  1566. PROCEDURE GotoDrop; (* fig. 29.27 *)
  1567. BEGIN
  1568. IF TraceProtocol THEN TraceTCP("Drop", p, empty^, empty^, 0, 0, 0) END;
  1569. IF drop THEN Drop(p, ConnectionAborted) END
  1570. END GotoDrop;
  1571. PROCEDURE GotoDropReset; (* fig. 29.27 - may be called with p = nilpcb (at start) *)
  1572. BEGIN
  1573. IF (Rst IN pf) THEN
  1574. GotoDrop
  1575. ELSE
  1576. IF Ack IN pf THEN
  1577. Respond(p, 0, ack, {Rst})
  1578. ELSE
  1579. IF Syn IN pf THEN INC(tlen) END;
  1580. Respond(p, seq + tlen, 0, {Rst,Ack})
  1581. END;
  1582. IF drop THEN Drop(p, ConnectionAborted) END
  1583. END
  1584. END GotoDropReset;
  1585. PROCEDURE GotoDropAfterAck; (* fig. 29.26 *)
  1586. BEGIN
  1587. IF Rst IN pf THEN
  1588. GotoDrop
  1589. ELSE
  1590. INCL(p.flags, AckNow); Output(p)
  1591. END
  1592. END GotoDropAfterAck;
  1593. PROCEDURE ProcessOptions; (* fig. 28.9 & 28.10 *)
  1594. VAR opt: CHAR; i, m, optlen: LONGINT;
  1595. BEGIN
  1596. i := buffer.ofs+MinTCPHdrLen; m := buffer.ofs+hdrLen;
  1597. LOOP
  1598. IF i >= m THEN EXIT END;
  1599. opt := buffer.data[i];
  1600. IF opt = 0X THEN
  1601. EXIT; (* EOL *)
  1602. ELSIF opt = 1X THEN
  1603. optlen := 1; (* NOP *)
  1604. ELSE
  1605. optlen := ORD(buffer.data[i+1]);
  1606. IF optlen = 0 THEN EXIT END;
  1607. END;
  1608. CASE opt OF
  1609. 2X: (* MaxSeg *)
  1610. IF (optlen = 4) & (Syn IN pf) THEN
  1611. SetMSS(p, Network.GetNet2(buffer.data, i+2));
  1612. END;
  1613. |3X: (* Window Scaling *)
  1614. IF (optlen = 3) & (Syn IN pf) THEN
  1615. INCL(p.flags, RcvdScale);
  1616. p.requestedsscale := MIN(LONG(ORD(buffer.data[i+2])), MaxWinShift);
  1617. END;
  1618. |8X: (* Timestamp *)
  1619. IF DoRFC1323 & (optlen = 10) THEN
  1620. INCL(lf, TSPresent);
  1621. tsval := Network.GetNet4(buffer.data, i+2);
  1622. tsecr := Network.GetNet4(buffer.data, i+6);
  1623. IF Syn IN pf THEN
  1624. INCL(p.flags, RcvdTstmp);
  1625. p.tsrecent := tsval;
  1626. p.tsrecentage := timeSource.now;
  1627. END;
  1628. END;
  1629. ELSE
  1630. (* skip *)
  1631. END;
  1632. INC(i, optlen);
  1633. END
  1634. END ProcessOptions;
  1635. PROCEDURE ProcessListen(): BOOLEAN; (* fig. 28.15-17 *)
  1636. VAR
  1637. res: WORD;
  1638. q: Connection;
  1639. BEGIN
  1640. IF Rst IN pf THEN GotoDrop; RETURN FALSE END;
  1641. IF Ack IN pf THEN GotoDropReset; RETURN FALSE END;
  1642. IF ~(Syn IN pf) THEN GotoDrop; RETURN FALSE END;
  1643. pool.Add(p, Network.GetNet2(buffer.data, buffer.ofs+2), Network.GetNet2(buffer.data, buffer.ofs), res);
  1644. IF res # Ok THEN
  1645. GotoDrop;
  1646. RETURN FALSE;
  1647. END;
  1648. IF ProcOptions & (Options IN lf) THEN ProcessOptions END;
  1649. p.iss := issSource.Get();
  1650. p.snduna := p.iss; p.sndnxt := p.iss; p.sndmax := p.iss; p.sndup := p.iss;
  1651. p.irs := seq; p.rcvnxt := seq+1; p.rcvadv := p.rcvnxt;
  1652. INCL(p.flags, AckNow);
  1653. p.state := SynReceived;
  1654. p.timer[Keep] := KeepInit;
  1655. drop := FALSE; (* commit *)
  1656. (* put on accept queue *)
  1657. ASSERT(Objects.LockedByCurrent(p.parent)); (* came here via Connection.Input of parent *)
  1658. q := p.parent.acceptNext; p.acceptNext := NIL;
  1659. IF q = NIL THEN
  1660. p.parent.acceptNext := p
  1661. ELSE
  1662. WHILE q.acceptNext # NIL DO q := q.acceptNext END; (* find last entry in queue *)
  1663. q.acceptNext := p
  1664. END;
  1665. Machine.AtomicInc(NTCPAccepts);
  1666. RETURN TRUE
  1667. END ProcessListen;
  1668. PROCEDURE ProcessSynSent(): BOOLEAN;
  1669. BEGIN
  1670. IF (Ack IN pf) & ((ack - p.iss <= 0) OR (ack - p.sndmax > 0)) THEN GotoDropReset; RETURN FALSE END;
  1671. IF Rst IN pf THEN
  1672. IF Ack IN pf THEN Error(ConnectionRefused, 0, p); Drop(p, ConnectionRefused) END;
  1673. GotoDrop; RETURN FALSE
  1674. END;
  1675. IF ~(Syn IN pf) THEN GotoDrop; RETURN FALSE END;
  1676. IF Ack IN pf THEN
  1677. p.snduna := ack;
  1678. IF p.sndnxt - p.snduna < 0 THEN p.sndnxt := p.snduna END;
  1679. END;
  1680. p.timer[ReXmt] := 0;
  1681. p.irs := seq; p.rcvnxt := seq+1; p.rcvadv := p.rcvnxt;
  1682. INCL(p.flags, AckNow);
  1683. IF (Ack IN pf) & (p.snduna - p.iss > 0) THEN
  1684. Machine.AtomicInc(NTCPConnects); (*SoIsConnected(p);*)
  1685. p.state := Established;
  1686. IF p.flags * {RcvdScale,ReqScale} = {RcvdScale,ReqScale} THEN
  1687. p.sndscale := p.requestedsscale; p.rcvscale := p.requestrscale
  1688. END;
  1689. (*GotoPresent;*) (* not necessary, processed later *)
  1690. IF p.rtt # 0 THEN XmitTimer(p, p.rtt) END
  1691. ELSE
  1692. p.state := SynReceived
  1693. END;
  1694. RETURN TRUE
  1695. END ProcessSynSent;
  1696. PROCEDURE Trim1; (* fig. 28.21 *)
  1697. BEGIN
  1698. INC(seq);
  1699. IF tlen > p.rcvwnd THEN
  1700. Error(DataBeyondWindow1, tlen - p.rcvwnd, p); (* data received beyond window (with Syn) *)
  1701. tlen := p.rcvwnd; EXCL(pf, Fin)
  1702. END;
  1703. p.sndwl1 := seq-1; p.rcvup := seq
  1704. END Trim1;
  1705. PROCEDURE Paws(): BOOLEAN; (* fig. 28.22 *)
  1706. BEGIN
  1707. IF (TSPresent IN lf) & ~(Rst IN pf) & (p.tsrecent # 0) & (tsval - p.tsrecent < 0) THEN
  1708. IF (timeSource.now - p.tsrecentage) > PawsIdle THEN
  1709. p.tsrecent := 0
  1710. ELSE
  1711. Error(DuplicateSegmentPAWS, tlen, p); (* duplicate segment (PAWS) *)
  1712. GotoDropAfterAck; RETURN FALSE
  1713. END
  1714. END;
  1715. RETURN TRUE
  1716. END Paws;
  1717. PROCEDURE Trim2(todrop: LONGINT): BOOLEAN; (* fig. 28.24-25, corrected fig. 28.30*)
  1718. BEGIN
  1719. IF Syn IN pf THEN
  1720. EXCL(pf, Syn); INC(seq);
  1721. IF urp > 1 THEN DEC(urp) ELSE EXCL(pf, Urg) END;
  1722. DEC(todrop)
  1723. END;
  1724. IF (todrop > tlen) OR ((todrop = tlen) & ~(Fin IN pf)) THEN
  1725. EXCL(pf, Fin); INCL(p.flags, AckNow); todrop := tlen;
  1726. Error(DuplicateSegment, todrop, p) (* duplicate segment *)
  1727. ELSE
  1728. Error(DuplicatePartialSegment, todrop, p) (* partially duplicate segment *)
  1729. END;
  1730. INC(discard, todrop); INC(seq, todrop); DEC(tlen, todrop);
  1731. IF urp > todrop THEN DEC(urp, todrop) ELSE EXCL(pf, Urg); urp := 0 END;
  1732. RETURN TRUE
  1733. END Trim2;
  1734. PROCEDURE Trim3(todrop: LONGINT): BOOLEAN; (* fig. 28.29 *)
  1735. BEGIN
  1736. IF todrop >= tlen THEN
  1737. IF (Syn IN pf) & (p.state = TimeWait) & (seq - p.rcvnxt > 0) THEN
  1738. (*iss := p.rcvnxt + ISSInc; CloseConnection(p); goto findpcb*)
  1739. Error(NIYNewIncarnation, 0, p); (* new incarnation NIY - also read p. 945-946 *)
  1740. GotoDropAfterAck; RETURN FALSE
  1741. END;
  1742. IF (p.rcvwnd = 0) & (seq = p.rcvnxt) THEN
  1743. INCL(p.flags, AckNow); Machine.AtomicInc(NTCPRcvWinProbe)
  1744. ELSE
  1745. Error(DataBeyondWindow2, tlen, p); (* data received beyond window (complete) *)
  1746. GotoDropAfterAck; RETURN FALSE
  1747. END
  1748. ELSE
  1749. Error(DataBeyondWindow3, todrop, p) (* data received beyond window (partial) *)
  1750. END;
  1751. DEC(tlen, todrop); pf := pf - {Psh,Fin};
  1752. RETURN TRUE
  1753. END Trim3;
  1754. PROCEDURE RecordTS; (* fig. 28.35 *)
  1755. VAR x: LONGINT;
  1756. BEGIN
  1757. IF DoRFC1323 THEN
  1758. IF pf * {Syn,Fin} # {} THEN x := 1 ELSE x := 0 END;
  1759. IF p.lastacksent - (seq + tlen + x) < 0 THEN
  1760. p.tsrecentage := timeSource.now;
  1761. p.tsrecent := tsval;
  1762. END
  1763. END
  1764. END RecordTS;
  1765. PROCEDURE ProcessRst(): BOOLEAN; (* fig. 28.36 *)
  1766. BEGIN
  1767. CASE p.state OF
  1768. SynReceived, Established, FinWait1, FinWait2, CloseWait:
  1769. IF p.state = SynReceived THEN p.error := ConnectionRefused ELSE p.error := ConnectionReset END;
  1770. p.state := Closed; Error(p.error, 0, p); (* connection reset *)
  1771. CloseConnection(p); GotoDrop; RETURN FALSE
  1772. |Closing, LastAck:
  1773. CloseConnection(p); GotoDrop; RETURN FALSE
  1774. ELSE (* skip *)
  1775. END;
  1776. RETURN TRUE
  1777. END ProcessRst;
  1778. PROCEDURE ProcessAck(): BOOLEAN; (* fig. 29.2-29.14 *)
  1779. VAR onxt, cw, incr: LONGINT; finacked: BOOLEAN;
  1780. BEGIN
  1781. IF p.state IN {SynReceived..TimeWait} THEN
  1782. IF p.state = SynReceived THEN
  1783. IF (p.snduna - ack > 0) OR (ack - p.sndmax > 0) THEN
  1784. GotoDropReset; RETURN FALSE
  1785. END;
  1786. Machine.AtomicInc(NTCPConnects);
  1787. (*SoIsConnected(p);*)
  1788. p.state := Established;
  1789. IF p.flags * {RcvdScale,ReqScale} = {RcvdScale,ReqScale} THEN
  1790. p.sndscale := p.requestedsscale; p.rcvscale := p.requestrscale
  1791. END;
  1792. (* IF tlen > 0 THEN (* would inc of twice by the header size *)
  1793. Reasm;
  1794. END; *)
  1795. p.sndwl1 := seq-1;
  1796. END;
  1797. IF ack - p.snduna <= 0 THEN
  1798. IF (tlen = 0) & (win = p.sndwnd) THEN
  1799. Error(DuplicateAck, 0, p); (* duplicate ack *)
  1800. IF (p.timer[ReXmt] = 0) OR (ack # p.snduna) THEN
  1801. p.dupacks := 0
  1802. ELSE
  1803. INC(p.dupacks);
  1804. IF p.dupacks = ReXmtThresh THEN
  1805. onxt := p.sndnxt;
  1806. p.sndssthresh := MAX(MIN(p.sndwnd, p.sndcwnd) DIV 2 DIV p.maxseg, 2) * p.maxseg;
  1807. IF TraceCongestion THEN
  1808. KernelLog.String("DA sndssthresh := "); KernelLog.Int(p.sndssthresh, 1); KernelLog.Ln
  1809. END;
  1810. p.timer[ReXmt] := 0; p.rtt := 0; p.sndnxt := ack; p.sndcwnd := p.maxseg;
  1811. Output(p);
  1812. p.sndcwnd := p.sndssthresh + p.maxseg * p.dupacks;
  1813. IF onxt - p.sndnxt > 0 THEN p.sndnxt := onxt END;
  1814. GotoDrop; RETURN FALSE
  1815. ELSIF p.dupacks > ReXmtThresh THEN
  1816. INC(p.sndcwnd, p.maxseg);
  1817. Output(p);
  1818. GotoDrop; RETURN FALSE
  1819. ELSE (* skip *)
  1820. END
  1821. END
  1822. ELSE
  1823. p.dupacks := 0
  1824. END;
  1825. RETURN TRUE (* skip rest of Ack processing - goto step 6 *)
  1826. END;
  1827. IF (p.dupacks > ReXmtThresh) & (p.sndcwnd > p.sndssthresh) THEN
  1828. p.sndcwnd := p.sndssthresh
  1829. END;
  1830. p.dupacks := 0;
  1831. IF ack - p.sndmax > 0 THEN
  1832. Error(OutOfRangeAck, ack - p.sndmax, p); GotoDropAfterAck;
  1833. RETURN FALSE;
  1834. END;
  1835. acked := ack - p.snduna;
  1836. Machine.AtomicInc(NTCPAckPack); Machine.AtomicAdd(NTCPAckByte, acked);
  1837. IF TSPresent IN lf THEN XmitTimer(p, timeSource.now - tsecr + 1)
  1838. ELSIF (p.rtt # 0) & (ack - p.rtseq > 0) THEN XmitTimer(p, p.rtt)
  1839. ELSE (* skip *)
  1840. END;
  1841. IF ack = p.sndmax THEN p.timer[ReXmt] := 0; INCL(lf, NeedOutput)
  1842. ELSIF p.timer[Persist] = 0 THEN
  1843. p.timer[ReXmt] := p.rxtcur;
  1844. ELSE (* skip *)
  1845. END;
  1846. cw := p.sndcwnd; incr := p.maxseg;
  1847. IF cw > p.sndssthresh THEN incr := incr * incr DIV cw END;
  1848. p.sndcwnd := MIN(cw + incr, ASH(MaxWin, p.sndscale));
  1849. IF acked > p.sndcc THEN
  1850. DEC(p.sndwnd, p.sndcc); SbDrop(p, p.sndcc); finacked := TRUE
  1851. ELSE
  1852. SbDrop(p, acked); DEC(p.sndwnd, acked); finacked := FALSE
  1853. END;
  1854. (*IF Notify IN p.flags THEN SoWakeup(p) END;*)
  1855. p.snduna := ack;
  1856. IF p.sndnxt - p.snduna < 0 THEN p.sndnxt := p.snduna END;
  1857. CASE p.state OF
  1858. FinWait1:
  1859. IF finacked THEN
  1860. (*IF NoMore IN p.flags THEN SoIsDisconnected(p); p.timer[MSL2] := MaxIdle END;*)
  1861. p.timer[MSL2] := MaxIdle; (* otherwise we hang in FinWait2 *)
  1862. p.state := FinWait2
  1863. END
  1864. |Closing:
  1865. IF finacked THEN
  1866. p.state := TimeWait; CancelTimers(p); p.timer[MSL2] := 2 * MSL;
  1867. (*SoIsDisconnected(p)*)
  1868. END
  1869. |LastAck:
  1870. IF finacked THEN
  1871. CloseConnection(p); GotoDrop; RETURN FALSE
  1872. END
  1873. |TimeWait:
  1874. p.timer[MSL2] := 2 * MSL; GotoDropAfterAck; RETURN FALSE
  1875. ELSE (* skip *)
  1876. END (* CASE *)
  1877. END;
  1878. RETURN TRUE
  1879. END ProcessAck;
  1880. PROCEDURE GotoPresent;
  1881. VAR
  1882. buf: Network.Buffer;
  1883. BEGIN
  1884. buf := p.rcvreasm; (* first buffer on reassembly list *)
  1885. IF (p.state >= Established) & (buf # NIL) & (buf.int = p.rcvnxt) THEN
  1886. REPEAT
  1887. DEC(p.rcvspace, buf.len);
  1888. INC(p.rcvnxt, buf.len);
  1889. pf := buf.set * {Fin};
  1890. buf := buf.next;
  1891. UNTIL (buf = NIL) OR (buf.int # p.rcvnxt);
  1892. p.rcvreasm := buf;
  1893. ELSE
  1894. EXCL(pf, Fin);
  1895. END;
  1896. END GotoPresent;
  1897. PROCEDURE Reasm;
  1898. VAR
  1899. pos, last: Network.Buffer;
  1900. lap: LONGINT;
  1901. BEGIN
  1902. buffer.set := pf;
  1903. buffer.int := seq;
  1904. INC(buffer.ofs, hdrLen + discard);
  1905. buffer.len := tlen;
  1906. IF p.rcvhead = NIL THEN
  1907. (* insert into empty queue *)
  1908. buffer.next := NIL;
  1909. buffer.prev := NIL;
  1910. p.rcvhead := buffer;
  1911. p.rcvreasm := buffer;
  1912. p.rcvtail := buffer;
  1913. bufferQueued := TRUE;
  1914. ELSE
  1915. (* go to insert position, insert in front of pos and after last. *)
  1916. pos := p.rcvreasm;
  1917. IF pos = NIL THEN
  1918. (* no reasm part of queue *)
  1919. last := p.rcvtail;
  1920. ELSE
  1921. last := pos.prev;
  1922. WHILE (pos # NIL) & (pos.int < seq) DO
  1923. last := pos;
  1924. pos := pos.next;
  1925. END;
  1926. END;
  1927. IF last # NIL THEN
  1928. (* check for overlap with previous buffer *)
  1929. lap := (last.int + last.len) - seq;
  1930. IF lap > 0 THEN
  1931. (* some overlap - drop new data *)
  1932. IF lap >= tlen THEN
  1933. (* complete duplicate *)
  1934. Error(DataDuplicatePrevComplete, tlen, p);
  1935. RETURN;
  1936. ELSE
  1937. (* partial duplicate *)
  1938. Error(DataDuplicatePrevPartial, lap, p);
  1939. INC(buffer.ofs, lap);
  1940. DEC(buffer.len, lap);
  1941. DEC(tlen, lap);
  1942. INC(seq, lap);
  1943. buffer.int := seq;
  1944. END;
  1945. END;
  1946. END;
  1947. IF pos # NIL THEN
  1948. (* check for overlap with next buffer *)
  1949. lap := (seq + tlen) - pos.int;
  1950. IF lap > 0 THEN
  1951. (* some overlap - drop new data *)
  1952. IF lap >= tlen THEN
  1953. (* complete duplicate *)
  1954. Error(DataDuplicateNextComplete, lap, p);
  1955. RETURN;
  1956. ELSE
  1957. (* partial duplicate *)
  1958. Error(DataDuplicateNextPartial, lap, p);
  1959. DEC(tlen, lap);
  1960. DEC(buffer.len, lap);
  1961. END;
  1962. END;
  1963. END;
  1964. Machine.AtomicInc(NTCPRcvPackSlow);
  1965. Machine.AtomicAdd(NTCPRcvByteSlow, tlen);
  1966. (* insert buffer into correct position in queue *)
  1967. IF pos = NIL THEN
  1968. (* insert at the end of the queue *)
  1969. ASSERT(last = p.rcvtail);
  1970. buffer.next := NIL;
  1971. buffer.prev := last;
  1972. buffer.prev.next := buffer;
  1973. p.rcvtail := buffer;
  1974. IF p.rcvreasm = NIL THEN
  1975. p.rcvreasm := buffer;
  1976. END;
  1977. ELSIF last = NIL THEN
  1978. (* insert at the beginning of the queue *)
  1979. ASSERT((pos = p.rcvhead) & (pos = p.rcvreasm));
  1980. buffer.prev := NIL;
  1981. buffer.next := pos;
  1982. buffer.next.prev := buffer;
  1983. p.rcvhead := buffer;
  1984. p.rcvreasm := buffer;
  1985. ELSE
  1986. (* insert somewhere in the middle *)
  1987. ASSERT((last.next = pos) & (pos.prev = last));
  1988. last.next := buffer;
  1989. buffer.prev := last;
  1990. pos.prev := buffer;
  1991. buffer.next := pos;
  1992. IF buffer.next = p.rcvreasm THEN
  1993. p.rcvreasm := buffer;
  1994. END;
  1995. END;
  1996. bufferQueued := TRUE;
  1997. END;
  1998. GotoPresent;
  1999. END Reasm;
  2000. PROCEDURE DoData;
  2001. BEGIN
  2002. IF ((tlen # 0) OR (Fin IN pf)) & (p.state < TimeWait) THEN
  2003. IF (seq = p.rcvnxt) & (p.rcvreasm = NIL) & (p.state = Established) THEN
  2004. INCL(p.flags, DelAck)
  2005. ELSE
  2006. INCL(p.flags, AckNow) (* cf. fig. 27.15 *)
  2007. END;
  2008. Reasm();
  2009. ELSE
  2010. EXCL(pf, Fin)
  2011. END;
  2012. IF Fin IN pf THEN
  2013. IF p.state < TimeWait THEN
  2014. (*SoCantRcvMore(p);*)
  2015. INCL(p.flags, AckNow); INC(p.rcvnxt)
  2016. END;
  2017. CASE p.state OF
  2018. SynReceived, Established:
  2019. p.state := CloseWait
  2020. |FinWait1:
  2021. p.state := Closing
  2022. |FinWait2:
  2023. p.state := TimeWait; CancelTimers(p); p.timer[MSL2] := 2 * MSL;
  2024. (*SoIsDisconnected(p)*)
  2025. |TimeWait:
  2026. p.timer[MSL2] := 2 * MSL
  2027. ELSE (* skip *)
  2028. END (* CASE *)
  2029. END;
  2030. IF TraceProtocol THEN
  2031. TraceTCP("Input", p, buffer.data, empty^, buffer.ofs+hdrLen, 0, 0)
  2032. END;
  2033. IF (NeedOutput IN lf) OR (AckNow IN p.flags) THEN Output(p) END
  2034. END DoData;
  2035. PROCEDURE Step6(): BOOLEAN;
  2036. BEGIN
  2037. IF (Ack IN pf) & ((p.sndwl1 - seq < 0) OR ((p.sndwl1 = seq) & ((p.sndwl2 - ack < 0) OR
  2038. ((p.sndwl2 = ack) & (win > p.sndwnd))))) THEN
  2039. IF (tlen = 0) & (p.sndwl2 = ack) & (win > p.sndwnd) THEN Machine.AtomicInc(NTCPRcvWinUpd) END;
  2040. p.sndwnd := win; p.sndwl1 := seq; p.sndwl2 := ack;
  2041. IF p.sndwnd > p.maxsndwnd THEN p.maxsndwnd := p.sndwnd END;
  2042. INCL(lf, NeedOutput)
  2043. END;
  2044. IF (Urg IN pf) & (urp # 0) & (p.state < TimeWait) THEN
  2045. Error(NIYOutOfBand, 0, p); (* out-of-band data NIY *)
  2046. (*IF urp + p.rcvcc > sbmax THEN*)
  2047. urp := 0; EXCL(pf, Urg); DoData; RETURN FALSE;
  2048. (*END;*)
  2049. ELSE
  2050. IF p.rcvnxt - p.rcvup > 0 THEN p.rcvup := p.rcvnxt END
  2051. END;
  2052. RETURN TRUE
  2053. END Step6;
  2054. BEGIN
  2055. lf := {};
  2056. discard := 0; (* data from 0 to discard has to be thrown away. *)
  2057. tlen := buffer.len-hdrLen; (* length of user data. *)
  2058. optLen := hdrLen - MinTCPHdrLen; (* length of options *)
  2059. pf := SYSTEM.VAL(SET, LONG(ORD(buffer.data[buffer.ofs+13])));
  2060. IF optLen > 0 THEN (* TCP options present *)
  2061. Machine.AtomicInc(NTCPRcvOptions);
  2062. IF ProcOptions THEN
  2063. IF DoRFC1323 THEN
  2064. (* quick processing of timestamp option, fig. 28.4 *)
  2065. IF ((optLen = 12) OR ((optLen > 12) & (buffer.data[buffer.ofs+MinTCPHdrLen+12] = 0X))) & ~(Syn IN pf) &
  2066. (buffer.data[buffer.ofs+MinTCPHdrLen] = 1X) &
  2067. (buffer.data[buffer.ofs+MinTCPHdrLen+1] = 1X) &
  2068. (buffer.data[buffer.ofs+MinTCPHdrLen+2] = 8X) &
  2069. (buffer.data[buffer.ofs+MinTCPHdrLen+3] = 0AX) THEN
  2070. INCL(lf, TSPresent);
  2071. tsval := Network.GetNet4(buffer.data, buffer.ofs+MinTCPHdrLen+4);
  2072. tsecr := Network.GetNet4(buffer.data, buffer.ofs+MinTCPHdrLen+8);
  2073. ELSE
  2074. INCL(lf, Options);
  2075. END;
  2076. ELSE
  2077. INCL(lf, Options);
  2078. END
  2079. END
  2080. END;
  2081. (* initialize variables needed for GotoDropReset *)
  2082. seq := Network.GetNet4(buffer.data, buffer.ofs+4);
  2083. ack := Network.GetNet4(buffer.data, buffer.ofs+8);
  2084. (* pf := SYSTEM.VAL(SET, LONG(ORD(buffer.data[buffer.ofs+13])));*)
  2085. IF p = nilpcb THEN
  2086. IF packetDumpListener # NIL THEN packetDumpListener(p.fip, buffer) END;
  2087. GotoDropReset; RETURN
  2088. END;
  2089. IF p.state <= Closed THEN GotoDrop; RETURN END;
  2090. IF ~(Network.ChecksumTCP IN buffer.calcChecksum) THEN
  2091. (* calculate checksum *)
  2092. (* set pseudo header *)
  2093. reassembledLength := 0;
  2094. fragmentBuffer := buffer;
  2095. WHILE fragmentBuffer # NIL DO
  2096. INC(reassembledLength, fragmentBuffer.len);
  2097. fragmentBuffer := fragmentBuffer.nextFragment;
  2098. END;
  2099. pseudoHdrLen := p.int.WritePseudoHeader(pseudoHdr, p.fip, p.int.localAdr, IPTypeTCP, reassembledLength);
  2100. sum := IP.Checksum1(pseudoHdr, 0, pseudoHdrLen, 0);
  2101. IF buffer.nextFragment # NIL THEN
  2102. (* fragmented packets *)
  2103. fragmentBuffer := buffer;
  2104. WHILE fragmentBuffer.nextFragment # NIL DO
  2105. sum := IP.Checksum1(fragmentBuffer.data, fragmentBuffer.ofs, fragmentBuffer.len, sum);
  2106. fragmentBuffer := fragmentBuffer.nextFragment;
  2107. END;
  2108. sum := IP.Checksum2(fragmentBuffer.data, fragmentBuffer.ofs, fragmentBuffer.len, sum);
  2109. ELSE
  2110. sum := IP.Checksum2(buffer.data, buffer.ofs, buffer.len, sum);
  2111. END;
  2112. IF sum # 0 THEN
  2113. Error(BadChecksum, 0, p);
  2114. GotoDrop;
  2115. RETURN;
  2116. END;
  2117. END;
  2118. win := Network.GetNet2(buffer.data, buffer.ofs+14);
  2119. urp := Network.GetNet2(buffer.data, buffer.ofs+18);
  2120. IF ~(Syn IN pf) THEN win := ASH(win, p.sndscale) END;
  2121. p.idle := 0; p.timer[Keep] := KeepIdle;
  2122. IF ProcOptions & (Options IN lf) & (p.state # Listen) THEN ProcessOptions END;
  2123. (* header prediction (fig. 28.11-13) *)
  2124. IF (p.state = Established) & (pf * {Syn,Fin,Rst,Urg,Ack} = {Ack}) &
  2125. (~DoRFC1323 OR ~(TSPresent IN lf) OR (tsval - p.tsrecent >= 0)) & (seq = p.rcvnxt) &
  2126. (win # 0) & (win = p.sndwnd) & (p.sndnxt = p.sndmax) THEN
  2127. IF DoRFC1323 & (TSPresent IN lf) & (seq - p.lastacksent <= 0) THEN
  2128. p.tsrecentage := timeSource.now;
  2129. p.tsrecent := tsval; (* see p. 937 & fig. 26.20 *)
  2130. END;
  2131. IF tlen = 0 THEN
  2132. IF (ack - p.snduna > 0) & (ack - p.sndmax <= 0) & (p.sndcwnd >= p.sndwnd) & (p.dupacks < ReXmtThresh) THEN
  2133. (* p.dupacks < ReXmtThres fix from "Performance Problems in 4.4BSD TCP" *)
  2134. Machine.AtomicInc(NTCPPredAck);
  2135. IF DoRFC1323 & (TSPresent IN lf) THEN XmitTimer(p, timeSource.now - tsecr + 1)
  2136. ELSIF (p.rtt # 0) & (ack - p.rtseq > 0) THEN XmitTimer(p, p.rtt)
  2137. ELSE (* skip *)
  2138. END;
  2139. acked := ack - p.snduna;
  2140. Machine.AtomicInc(NTCPAckPack); Machine.AtomicAdd(NTCPAckByte, acked);
  2141. SbDrop(p, acked);
  2142. p.snduna := ack;
  2143. IF ack = p.sndmax THEN p.timer[ReXmt] := 0
  2144. ELSIF p.timer[Persist] = 0 THEN
  2145. p.timer[ReXmt] := p.rxtcur;
  2146. ELSE (* skip *)
  2147. END;
  2148. (*IF Notify IN p.flags THEN SoWakeup(p) END;*)
  2149. IF p.sndcc # 0 THEN Output(p) END;
  2150. RETURN
  2151. END
  2152. ELSIF (ack = p.snduna) & (p.rcvreasm = NIL) & (tlen <= p.rcvspace) THEN
  2153. Machine.AtomicInc(NTCPPredData);
  2154. Machine.AtomicInc(NTCPRcvPackFast);
  2155. Machine.AtomicAdd(NTCPRcvByteFast, tlen);
  2156. (* can assume no overlap with last or next buffers *)
  2157. Reasm; (* queue the buffer directly *)
  2158. (*SoWakeup(p);*)
  2159. INCL(p.flags, DelAck);
  2160. RETURN
  2161. ELSE
  2162. (* skip - continue to slow path *)
  2163. END
  2164. END;
  2165. (* slow path *)
  2166. p.rcvwnd := MAX(MAX(p.rcvspace, 0), p.rcvadv - p.rcvnxt);
  2167. CASE p.state OF
  2168. Listen:
  2169. IF ProcessListen() THEN Trim1 ELSE RETURN END
  2170. |SynSent:
  2171. IF ProcessSynSent() THEN Trim1 ELSE RETURN END
  2172. ELSE
  2173. IF DoRFC1323 & ~Paws() THEN RETURN END;
  2174. IF (p.rcvnxt - seq > 0) & ~Trim2(p.rcvnxt - seq) THEN RETURN END;
  2175. (*IF (Gone IN p.flags) & (p.state > CloseWait) & (tlen # 0) THEN
  2176. CloseConnection(p); Machine.AtomicInc(NTCPRcvAfterClose); GotoDropReset; RETURN
  2177. END;*)
  2178. IF ((seq + tlen) - (p.rcvnxt + p.rcvwnd) > 0) & ~Trim3((seq + tlen) - (p.rcvnxt + p.rcvwnd)) THEN RETURN END;
  2179. IF DoRFC1323 & (TSPresent IN lf) & (seq - p.lastacksent <= 0) THEN RecordTS END;
  2180. IF (Rst IN pf) & ~ProcessRst() THEN RETURN END;
  2181. IF Syn IN pf THEN Drop(p, ConnectionReset); GotoDropReset; RETURN END;
  2182. IF ~(Ack IN pf) THEN GotoDrop; RETURN END;
  2183. IF ~ProcessAck() THEN RETURN END;
  2184. END; (* CASE *)
  2185. IF ~Step6() THEN RETURN END;
  2186. DoData;
  2187. END ProcessInput;
  2188. (* Initialize a new Connection. *)
  2189. PROCEDURE InitConnection(p: Connection);
  2190. VAR
  2191. buf: SendBuffer;
  2192. BEGIN
  2193. IF ~NewZeros THEN (* clear all fields *)
  2194. CancelTimers(p);
  2195. p.fip := IP.NilAdr; p.fport := NilPort; p.rxtshift := 0; p.dupacks := 0;
  2196. p.snduna := 0; p.sndnxt := 0; p.sndup := 0; p.sndwl1 := 0; p.sndwl2 := 0; p.iss := 0;
  2197. p.sndwnd := 0; p.rcvwnd := 0; p.rcvnxt := 0; p.rcvup := 0; p.irs := 0; p.rcvadv := 0;
  2198. p.sndmax := 0; p.idle := 0; p.rtt := 0; p.rtseq := 0; p.maxsndwnd := 0;
  2199. p.sndscale := 0; p.rcvscale := 0; p.requestrscale := 0; p.requestedsscale := 0;
  2200. p.tsrecent := 0; p.tsrecentage := 0; p.lastacksent := 0; p.error := 0; p.acceptable := 0;
  2201. p.sndcc := 0; p.poolNext := NIL; p.parent := NIL; p.acceptNext := NIL
  2202. END;
  2203. (* initialize fields *)
  2204. p.maxseg := MSS; p.state := Closed;
  2205. IF DoRFC1323 THEN p.flags := {ReqScale,ReqTstmp} ELSE p.flags := {} END;
  2206. p.srtt := SRTTBase; p.rttvar := SRTTDflt * 4; p.rttmin := MinTime;
  2207. RangeSet(p.rxtcur, (SRTTBase DIV 4 + SRTTDflt * 4) DIV 2, MinTime, ReXmtMax);
  2208. p.sndcwnd := ASH(MaxWin, MaxWinShift);
  2209. p.sndssthresh := ASH(MaxWin, MaxWinShift);
  2210. p.sndspace := MaxSendSpace;
  2211. p.rcvspace := MaxRecvSpace;
  2212. p.rcvhiwat := MaxRecvSpace;
  2213. WHILE (p.requestrscale < MaxWinShift) & (ASH(MaxWin, p.requestrscale) < p.rcvhiwat) DO
  2214. INC(p.requestrscale)
  2215. END;
  2216. Machine.AtomicInc(NTCPNewBufs);
  2217. (* allocate send buffer *)
  2218. NEW(buf); NEW(buf.data, MSS * SegsPerBuf);
  2219. IF ~NewZeros THEN buf.ofs := 0; buf.len := 0; END;
  2220. buf.next := buf; p.sndhead := buf; p.sndtail := buf;
  2221. NEW(p.sndcontig, MSS);
  2222. (* init receive buffer *)
  2223. p.rcvhead := NIL;
  2224. END InitConnection;
  2225. (* Move connection to next state in close process. *)
  2226. PROCEDURE UsrClosed(p: Connection);
  2227. BEGIN
  2228. CASE p.state OF
  2229. Closed, Listen, SynSent:
  2230. p.state := Closed; CloseConnection(p)
  2231. |SynReceived, Established:
  2232. p.state := FinWait1
  2233. |CloseWait:
  2234. p.state := LastAck
  2235. ELSE (* skip *)
  2236. END;
  2237. (*IF p.state >= FinWait2 THEN SoIsDisconnected(p) END*)
  2238. END UsrClosed;
  2239. (* If t is not zero, decrement it. Return true if it has reached 0, false otherwise. *)
  2240. PROCEDURE Expired(VAR t: LONGINT): BOOLEAN;
  2241. BEGIN
  2242. IF t # 0 THEN DEC(t); RETURN t = 0 ELSE RETURN FALSE END
  2243. END Expired;
  2244. PROCEDURE GetID(): LONGINT;
  2245. VAR id, time, date: LONGINT;
  2246. BEGIN
  2247. Clock.Get(time, date);
  2248. id := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, time) / SYSTEM.VAL(SET, date));
  2249. RETURN id;
  2250. END GetID;
  2251. (** Aos command - display all errors *)
  2252. PROCEDURE DisplayErrors*(context : Commands.Context);
  2253. VAR i: LONGINT;
  2254. BEGIN
  2255. FOR i := 1 TO NumErrors-1 DO
  2256. IF NTCPError[i] # 0 THEN
  2257. context.out.String("TCP: Error "); context.out.Int(i+MinError, 1);
  2258. context.out.String(" "); context.out.Int(NTCPError[i], 1); context.out.Ln;
  2259. END;
  2260. END;
  2261. END DisplayErrors;
  2262. (** Aos command - discard and finalize all connections *)
  2263. PROCEDURE DiscardAll*;
  2264. BEGIN
  2265. pool.Finalize();
  2266. END DiscardAll;
  2267. (** Temporary trace procedure. *)
  2268. PROCEDURE ToggleTrace*;
  2269. BEGIN
  2270. trace := ~trace;
  2271. KernelLog.Enter; KernelLog.String("TCP trace ");
  2272. IF trace THEN KernelLog.String("on") ELSE KernelLog.String("off") END;
  2273. KernelLog.Exit
  2274. END ToggleTrace;
  2275. PROCEDURE Init;
  2276. VAR i: LONGINT;
  2277. BEGIN
  2278. FOR i := 0 TO MaxRxtShift DO
  2279. IF i < 6 THEN backoff[i] := ASH(1, i) ELSE backoff[i] := 64 END;
  2280. INC(totbackoff, backoff[i])
  2281. END;
  2282. (* maxdiff := 0;*)
  2283. (* backoff[0]:=1;
  2284. backoff[1]:=1;
  2285. backoff[2]:=2;
  2286. backoff[3]:=3;
  2287. backoff[4]:=5;
  2288. backoff[5]:=10;
  2289. backoff[6]:=18;
  2290. backoff[7]:=30;
  2291. backoff[8]:=60;
  2292. backoff[9]:=120;
  2293. backoff[10]:=240;
  2294. backoff[11]:=240;
  2295. backoff[12]:=240;*)
  2296. ASSERT(MaxRxtShift=12);
  2297. totbackoff := 0;
  2298. FOR i:=0 TO MaxRxtShift DO
  2299. INC(totbackoff, backoff[i]);
  2300. END;
  2301. (* Flags used when sending segments in tcp_output. Basic flags {Rst, Ack, Syn, Fin} are totally determined by state, with the proviso that Fin is sent only if all data queued for output is included in the segment. The {Psh,Urg} flags are set as necessary.
  2302. *)
  2303. outflags[Closed] := {Rst,Ack}; outflags[Listen] := {}; outflags[SynSent] := {Syn};
  2304. outflags[SynReceived] := {Syn,Ack}; outflags[Established] := {Ack};
  2305. outflags[CloseWait] := {Ack}; outflags[FinWait1] := {Fin,Ack}; outflags[Closing] := {Fin,Ack};
  2306. outflags[LastAck] := {Fin,Ack}; outflags[FinWait2] := {Ack}; outflags[TimeWait] := {Ack};
  2307. (* other globals *)
  2308. NEW(nilpcb);
  2309. nilpcb.lport := -1; (* can never match *)
  2310. nilpcb.int := NIL;
  2311. IF ~NewZeros THEN nilpcb.rcvspace := 0; nilpcb.rcvscale := 0 END; (* for Respond *)
  2312. lastpcb := nilpcb;
  2313. NEW(empty, 1);
  2314. NEW(pool);
  2315. NEW(issSource, GetID());
  2316. NEW(timeSource)
  2317. END Init;
  2318. PROCEDURE Cleanup;
  2319. BEGIN
  2320. IP.RemoveReceiver(IPTypeTCP);
  2321. pool.Finalize();
  2322. timeSource.Finalize();
  2323. END Cleanup;
  2324. BEGIN
  2325. ASSERT(~DoRFC1323 OR (ProcOptions & GenOptions)); (* constants should make sense *)
  2326. ASSERT(SYSTEM.VAL(LONGINT, {Fin}) = 1); (* bit order for flags cast in Input, Output and Respond *)
  2327. ASSERT((TimerPeriod MOD FastPeriod = 0) & (TimerPeriod MOD SlowPeriod = 0));
  2328. trace := FALSE;
  2329. Init();
  2330. IP.InstallReceiver(IPTypeTCP, Input);
  2331. Modules.InstallTermHandler(Cleanup);
  2332. END TCP.
  2333. (*
  2334. History:
  2335. 08.11.2003 mvt Changed for new interface of IP and Network.
  2336. 08.11.2003 mvt Fixed array position error in ProcessOptions().
  2337. 09.11.2003 mvt Min()/Max() functions now in inline assembler.
  2338. 10.11.2003 mvt Added InterfaceClosed detection in Connection.SlowTimer().
  2339. 10.11.2003 mvt Added correct finalization of Connection, ConnectionPool, Timer and the module itself.
  2340. 11.11.2003 mvt Completely changed receive buffer queueing, integrated Network buffers directly.
  2341. 12.11.2003 mvt Completely changed ConnectionPool, now working with a hash table.
  2342. 12.12.2003 mvt Bugfixed entire module by comparing it with the book.
  2343. 12.12.2003 mvt Completed header prediction code according to the book.
  2344. 12.12.2003 mvt Added support for RFC1323 (timestamp, PAWS).
  2345. 12.12.2003 mvt Added support for window scaling (for windows >64KB).
  2346. 14.02.2004 mvt Fixed reassembly bug in Reasm().
  2347. 14.02.2004 mvt Fixed MSS option sending bug in Output().
  2348. 28.02.2004 mvt Fixed early Fin sending bug in Output().
  2349. 04.03.2004 rp Fixed & (p.dupacks < ReXmtThresh) in Step6 according to ftp://ftp.cs.arizona.edu/xkernel/papers/tcp_problems.ps
  2350. 04.03.2004 rp Fixed XmitTimer according to ftp://ftp.cs.arizona.edu/xkernel/papers/tcp_problems.ps
  2351. 02.05.2005 eb Supports IPv6 (WritePseudoHdr) and fragmented IP packets.
  2352. *)
  2353. (*
  2354. * Copyright (c) 1982, 1986, 1993
  2355. * The Regents of the University of California. All rights reserved.
  2356. *
  2357. * Redistribution and use in source and binary forms, with or without
  2358. * modification, are permitted provided that the following conditions
  2359. * are met:
  2360. * 1. Redistributions of source code must retain the above copyright
  2361. * notice, this list of conditions and the following disclaimer.
  2362. * 2. Redistributions in binary form must reproduce the above copyright
  2363. * notice, this list of conditions and the following disclaimer in the
  2364. * documentation and/or other materials provided with the distribution.
  2365. * 3. All advertising materials mentioning features or use of this software
  2366. * must display the following acknowledgement:
  2367. * This product includes software developed by the University of
  2368. * California, Berkeley and its contributors.
  2369. * 4. Neither the name of the University nor the names of its contributors
  2370. * may be used to endorse or promote products derived from this software
  2371. * without specific prior written permission.
  2372. *
  2373. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  2374. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  2375. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  2376. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  2377. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  2378. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  2379. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  2380. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  2381. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  2382. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  2383. * SUCH DAMAGE.
  2384. *)
  2385. (* pjm:
  2386. Gigabit:
  2387. Alteon AceNIC / 3Com 3C985 / NetGear GA620 Gigabit Ethernet Adapter
  2388. http://sanjose.alteon.com/open.shtml
  2389. Linux Driver
  2390. http://home.cern.ch/~jes/gige/acenic.html
  2391. Packet Engines Hamachi Driver
  2392. http://www.nscl.msu.edu/~kasten/perf/hamachi/
  2393. A Connection has to react to external and internal events
  2394. 1. User calls (external)
  2395. Connection.Open
  2396. Connection.Send
  2397. Connection.Receive
  2398. Connection.Available
  2399. Connection.Close
  2400. 2. Timer.HandleTimeout (internal)
  2401. Connection.DelayedAck
  2402. Connection.SlowTimer
  2403. 3. Packet arrival (external)
  2404. Connection.Input (from Input)
  2405. The Timer reacts to external and internal events
  2406. 1. Timer.HandleTimeout (external)
  2407. 2. Timer.GetISS (internal - only called from Connection.Open, which is only called externally)
  2408. The ConnectionPool reacts to external and internal events
  2409. 1. Lookup (internal)
  2410. 2. Enumerate (external)
  2411. 3. Add (internal)
  2412. 4. Remove (internal)
  2413. *)