association.go 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package sctp
  4. import (
  5. "bytes"
  6. "context"
  7. "errors"
  8. "fmt"
  9. "io"
  10. "math"
  11. "net"
  12. "sync"
  13. "sync/atomic"
  14. "time"
  15. "github.com/pion/logging"
  16. "github.com/pion/randutil"
  17. )
  18. // Port 5000 shows up in examples for SDPs used by WebRTC. Since this implementation
  19. // assumes it will be used by DTLS over UDP, the port is only meaningful for de-multiplexing
  20. // but more-so verification.
  21. // Example usage: https://www.rfc-editor.org/rfc/rfc8841.html#section-13.1-2
  22. const defaultSCTPSrcDstPort = 5000
  23. // Use global random generator to properly seed by crypto grade random.
  24. var globalMathRandomGenerator = randutil.NewMathRandomGenerator() // nolint:gochecknoglobals
  25. // Association errors
  26. var (
  27. ErrChunk = errors.New("abort chunk, with following errors")
  28. ErrShutdownNonEstablished = errors.New("shutdown called in non-established state")
  29. ErrAssociationClosedBeforeConn = errors.New("association closed before connecting")
  30. ErrSilentlyDiscard = errors.New("silently discard")
  31. ErrInitNotStoredToSend = errors.New("the init not stored to send")
  32. ErrCookieEchoNotStoredToSend = errors.New("cookieEcho not stored to send")
  33. ErrSCTPPacketSourcePortZero = errors.New("sctp packet must not have a source port of 0")
  34. ErrSCTPPacketDestinationPortZero = errors.New("sctp packet must not have a destination port of 0")
  35. ErrInitChunkBundled = errors.New("init chunk must not be bundled with any other chunk")
  36. ErrInitChunkVerifyTagNotZero = errors.New("init chunk expects a verification tag of 0 on the packet when out-of-the-blue")
  37. ErrHandleInitState = errors.New("todo: handle Init when in state")
  38. ErrInitAckNoCookie = errors.New("no cookie in InitAck")
  39. ErrInflightQueueTSNPop = errors.New("unable to be popped from inflight queue TSN")
  40. ErrTSNRequestNotExist = errors.New("requested non-existent TSN")
  41. ErrResetPacketInStateNotExist = errors.New("sending reset packet in non-established state")
  42. ErrParamterType = errors.New("unexpected parameter type")
  43. ErrPayloadDataStateNotExist = errors.New("sending payload data in non-established state")
  44. ErrChunkTypeUnhandled = errors.New("unhandled chunk type")
  45. ErrHandshakeInitAck = errors.New("handshake failed (INIT ACK)")
  46. ErrHandshakeCookieEcho = errors.New("handshake failed (COOKIE ECHO)")
  47. ErrTooManyReconfigRequests = errors.New("too many outstanding reconfig requests")
  48. )
  49. const (
  50. receiveMTU uint32 = 8192 // MTU for inbound packet (from DTLS)
  51. initialMTU uint32 = 1228 // initial MTU for outgoing packets (to DTLS)
  52. initialRecvBufSize uint32 = 1024 * 1024
  53. commonHeaderSize uint32 = 12
  54. dataChunkHeaderSize uint32 = 16
  55. defaultMaxMessageSize uint32 = 65536
  56. )
  57. // association state enums
  58. const (
  59. closed uint32 = iota
  60. cookieWait
  61. cookieEchoed
  62. established
  63. shutdownAckSent
  64. shutdownPending
  65. shutdownReceived
  66. shutdownSent
  67. )
  68. // retransmission timer IDs
  69. const (
  70. timerT1Init int = iota
  71. timerT1Cookie
  72. timerT2Shutdown
  73. timerT3RTX
  74. timerReconfig
  75. )
  76. // ack mode (for testing)
  77. const (
  78. ackModeNormal int = iota
  79. ackModeNoDelay
  80. ackModeAlwaysDelay
  81. )
  82. // ack transmission state
  83. const (
  84. ackStateIdle int = iota // ack timer is off
  85. ackStateImmediate // will send ack immediately
  86. ackStateDelay // ack timer is on (ack is being delayed)
  87. )
  88. // other constants
  89. const (
  90. acceptChSize = 16
  91. // avgChunkSize is an estimate of the average chunk size. There is no theory behind
  92. // this estimate.
  93. avgChunkSize = 500
  94. // minTSNOffset is the minimum offset over the cummulative TSN that we will enqueue
  95. // irrespective of the receive buffer size
  96. // see Association.getMaxTSNOffset
  97. minTSNOffset = 2000
  98. // maxTSNOffset is the maximum offset over the cummulative TSN that we will enqueue
  99. // irrespective of the receive buffer size
  100. // see Association.getMaxTSNOffset
  101. maxTSNOffset = 40000
  102. // maxReconfigRequests is the maximum number of reconfig requests we will keep outstanding
  103. maxReconfigRequests = 1000
  104. )
  105. func getAssociationStateString(a uint32) string {
  106. switch a {
  107. case closed:
  108. return "Closed"
  109. case cookieWait:
  110. return "CookieWait"
  111. case cookieEchoed:
  112. return "CookieEchoed"
  113. case established:
  114. return "Established"
  115. case shutdownPending:
  116. return "ShutdownPending"
  117. case shutdownSent:
  118. return "ShutdownSent"
  119. case shutdownReceived:
  120. return "ShutdownReceived"
  121. case shutdownAckSent:
  122. return "ShutdownAckSent"
  123. default:
  124. return fmt.Sprintf("Invalid association state %d", a)
  125. }
  126. }
  127. // Association represents an SCTP association
  128. // 13.2. Parameters Necessary per Association (i.e., the TCB)
  129. //
  130. // Peer : Tag value to be sent in every packet and is received
  131. // Verification: in the INIT or INIT ACK chunk.
  132. // Tag :
  133. // State : A state variable indicating what state the association
  134. // : is in, i.e., COOKIE-WAIT, COOKIE-ECHOED, ESTABLISHED,
  135. // : SHUTDOWN-PENDING, SHUTDOWN-SENT, SHUTDOWN-RECEIVED,
  136. // : SHUTDOWN-ACK-SENT.
  137. //
  138. // Note: No "CLOSED" state is illustrated since if a
  139. // association is "CLOSED" its TCB SHOULD be removed.
  140. // Note: By nature of an Association being constructed with one net.Conn,
  141. // it is not a multi-home supporting implementation of SCTP.
  142. type Association struct {
  143. bytesReceived uint64
  144. bytesSent uint64
  145. lock sync.RWMutex
  146. netConn net.Conn
  147. peerVerificationTag uint32
  148. myVerificationTag uint32
  149. state uint32
  150. initialTSN uint32
  151. myNextTSN uint32 // nextTSN
  152. peerLastTSN uint32 // lastRcvdTSN
  153. minTSN2MeasureRTT uint32 // for RTT measurement
  154. willSendForwardTSN bool
  155. willRetransmitFast bool
  156. willRetransmitReconfig bool
  157. willSendShutdown bool
  158. willSendShutdownAck bool
  159. willSendShutdownComplete bool
  160. willSendAbort bool
  161. willSendAbortCause errorCause
  162. // Reconfig
  163. myNextRSN uint32
  164. reconfigs map[uint32]*chunkReconfig
  165. reconfigRequests map[uint32]*paramOutgoingResetRequest
  166. // Non-RFC internal data
  167. sourcePort uint16
  168. destinationPort uint16
  169. myMaxNumInboundStreams uint16
  170. myMaxNumOutboundStreams uint16
  171. myCookie *paramStateCookie
  172. payloadQueue *payloadQueue
  173. inflightQueue *payloadQueue
  174. pendingQueue *pendingQueue
  175. controlQueue *controlQueue
  176. mtu uint32
  177. maxPayloadSize uint32 // max DATA chunk payload size
  178. srtt atomic.Value // type float64
  179. cumulativeTSNAckPoint uint32
  180. advancedPeerTSNAckPoint uint32
  181. useForwardTSN bool
  182. sendZeroChecksum bool
  183. recvZeroChecksum bool
  184. // Congestion control parameters
  185. maxReceiveBufferSize uint32
  186. maxMessageSize uint32
  187. cwnd uint32 // my congestion window size
  188. rwnd uint32 // calculated peer's receiver windows size
  189. ssthresh uint32 // slow start threshold
  190. partialBytesAcked uint32
  191. inFastRecovery bool
  192. fastRecoverExitPoint uint32
  193. // RTX & Ack timer
  194. rtoMgr *rtoManager
  195. t1Init *rtxTimer
  196. t1Cookie *rtxTimer
  197. t2Shutdown *rtxTimer
  198. t3RTX *rtxTimer
  199. tReconfig *rtxTimer
  200. ackTimer *ackTimer
  201. // Chunks stored for retransmission
  202. storedInit *chunkInit
  203. storedCookieEcho *chunkCookieEcho
  204. streams map[uint16]*Stream
  205. acceptCh chan *Stream
  206. readLoopCloseCh chan struct{}
  207. awakeWriteLoopCh chan struct{}
  208. closeWriteLoopCh chan struct{}
  209. handshakeCompletedCh chan error
  210. closeWriteLoopOnce sync.Once
  211. // local error
  212. silentError error
  213. ackState int
  214. ackMode int // for testing
  215. // stats
  216. stats *associationStats
  217. // per inbound packet context
  218. delayedAckTriggered bool
  219. immediateAckTriggered bool
  220. name string
  221. log logging.LeveledLogger
  222. }
  223. // Config collects the arguments to createAssociation construction into
  224. // a single structure
  225. type Config struct {
  226. Name string
  227. NetConn net.Conn
  228. MaxReceiveBufferSize uint32
  229. MaxMessageSize uint32
  230. EnableZeroChecksum bool
  231. LoggerFactory logging.LoggerFactory
  232. // RTOMax is the maximum retransmission timeout in milliseconds
  233. RTOMax float64
  234. }
  235. // Server accepts a SCTP stream over a conn
  236. func Server(config Config) (*Association, error) {
  237. a := createAssociation(config)
  238. a.init(false)
  239. select {
  240. case err := <-a.handshakeCompletedCh:
  241. if err != nil {
  242. return nil, err
  243. }
  244. return a, nil
  245. case <-a.readLoopCloseCh:
  246. return nil, ErrAssociationClosedBeforeConn
  247. }
  248. }
  249. // Client opens a SCTP stream over a conn
  250. func Client(config Config) (*Association, error) {
  251. return createClientWithContext(context.Background(), config)
  252. }
  253. func createClientWithContext(ctx context.Context, config Config) (*Association, error) {
  254. a := createAssociation(config)
  255. a.init(true)
  256. select {
  257. case <-ctx.Done():
  258. a.log.Errorf("[%s] client handshake canceled: state=%s", a.name, getAssociationStateString(a.getState()))
  259. a.Close() // nolint:errcheck,gosec
  260. return nil, ctx.Err()
  261. case err := <-a.handshakeCompletedCh:
  262. if err != nil {
  263. return nil, err
  264. }
  265. return a, nil
  266. case <-a.readLoopCloseCh:
  267. return nil, ErrAssociationClosedBeforeConn
  268. }
  269. }
  270. func createAssociation(config Config) *Association {
  271. var maxReceiveBufferSize uint32
  272. if config.MaxReceiveBufferSize == 0 {
  273. maxReceiveBufferSize = initialRecvBufSize
  274. } else {
  275. maxReceiveBufferSize = config.MaxReceiveBufferSize
  276. }
  277. var maxMessageSize uint32
  278. if config.MaxMessageSize == 0 {
  279. maxMessageSize = defaultMaxMessageSize
  280. } else {
  281. maxMessageSize = config.MaxMessageSize
  282. }
  283. tsn := globalMathRandomGenerator.Uint32()
  284. a := &Association{
  285. netConn: config.NetConn,
  286. maxReceiveBufferSize: maxReceiveBufferSize,
  287. maxMessageSize: maxMessageSize,
  288. // These two max values have us not need to follow
  289. // 5.1.1 where this peer may be incapable of supporting
  290. // the requested amount of outbound streams from the other
  291. // peer.
  292. myMaxNumOutboundStreams: math.MaxUint16,
  293. myMaxNumInboundStreams: math.MaxUint16,
  294. payloadQueue: newPayloadQueue(),
  295. inflightQueue: newPayloadQueue(),
  296. pendingQueue: newPendingQueue(),
  297. controlQueue: newControlQueue(),
  298. mtu: initialMTU,
  299. maxPayloadSize: initialMTU - (commonHeaderSize + dataChunkHeaderSize),
  300. myVerificationTag: globalMathRandomGenerator.Uint32(),
  301. initialTSN: tsn,
  302. myNextTSN: tsn,
  303. myNextRSN: tsn,
  304. minTSN2MeasureRTT: tsn,
  305. state: closed,
  306. rtoMgr: newRTOManager(config.RTOMax),
  307. streams: map[uint16]*Stream{},
  308. reconfigs: map[uint32]*chunkReconfig{},
  309. reconfigRequests: map[uint32]*paramOutgoingResetRequest{},
  310. acceptCh: make(chan *Stream, acceptChSize),
  311. readLoopCloseCh: make(chan struct{}),
  312. awakeWriteLoopCh: make(chan struct{}, 1),
  313. closeWriteLoopCh: make(chan struct{}),
  314. handshakeCompletedCh: make(chan error),
  315. cumulativeTSNAckPoint: tsn - 1,
  316. advancedPeerTSNAckPoint: tsn - 1,
  317. recvZeroChecksum: config.EnableZeroChecksum,
  318. silentError: ErrSilentlyDiscard,
  319. stats: &associationStats{},
  320. log: config.LoggerFactory.NewLogger("sctp"),
  321. name: config.Name,
  322. }
  323. if a.name == "" {
  324. a.name = fmt.Sprintf("%p", a)
  325. }
  326. // RFC 4690 Sec 7.2.1
  327. // o The initial cwnd before DATA transmission or after a sufficiently
  328. // long idle period MUST be set to min(4*MTU, max (2*MTU, 4380
  329. // bytes)).
  330. a.setCWND(min32(4*a.MTU(), max32(2*a.MTU(), 4380)))
  331. a.log.Tracef("[%s] updated cwnd=%d ssthresh=%d inflight=%d (INI)",
  332. a.name, a.CWND(), a.ssthresh, a.inflightQueue.getNumBytes())
  333. a.srtt.Store(float64(0))
  334. a.t1Init = newRTXTimer(timerT1Init, a, maxInitRetrans, config.RTOMax)
  335. a.t1Cookie = newRTXTimer(timerT1Cookie, a, maxInitRetrans, config.RTOMax)
  336. a.t2Shutdown = newRTXTimer(timerT2Shutdown, a, noMaxRetrans, config.RTOMax)
  337. a.t3RTX = newRTXTimer(timerT3RTX, a, noMaxRetrans, config.RTOMax)
  338. a.tReconfig = newRTXTimer(timerReconfig, a, noMaxRetrans, config.RTOMax)
  339. a.ackTimer = newAckTimer(a)
  340. return a
  341. }
  342. func (a *Association) init(isClient bool) {
  343. a.lock.Lock()
  344. defer a.lock.Unlock()
  345. go a.readLoop()
  346. go a.writeLoop()
  347. if isClient {
  348. init := &chunkInit{}
  349. init.initialTSN = a.myNextTSN
  350. init.numOutboundStreams = a.myMaxNumOutboundStreams
  351. init.numInboundStreams = a.myMaxNumInboundStreams
  352. init.initiateTag = a.myVerificationTag
  353. init.advertisedReceiverWindowCredit = a.maxReceiveBufferSize
  354. setSupportedExtensions(&init.chunkInitCommon)
  355. if a.recvZeroChecksum {
  356. init.params = append(init.params, &paramZeroChecksumAcceptable{edmid: dtlsErrorDetectionMethod})
  357. }
  358. a.storedInit = init
  359. err := a.sendInit()
  360. if err != nil {
  361. a.log.Errorf("[%s] failed to send init: %s", a.name, err.Error())
  362. }
  363. // After sending the INIT chunk, "A" starts the T1-init timer and enters the COOKIE-WAIT state.
  364. // Note: ideally we would set state after the timer starts but since we don't do this in an atomic
  365. // set + timer-start, it's safer to just set the state first so that we don't have a timer expiration
  366. // race.
  367. a.setState(cookieWait)
  368. a.t1Init.start(a.rtoMgr.getRTO())
  369. }
  370. }
  371. // caller must hold a.lock
  372. func (a *Association) sendInit() error {
  373. a.log.Debugf("[%s] sending INIT", a.name)
  374. if a.storedInit == nil {
  375. return ErrInitNotStoredToSend
  376. }
  377. outbound := &packet{}
  378. outbound.verificationTag = a.peerVerificationTag
  379. a.sourcePort = defaultSCTPSrcDstPort
  380. a.destinationPort = defaultSCTPSrcDstPort
  381. outbound.sourcePort = a.sourcePort
  382. outbound.destinationPort = a.destinationPort
  383. outbound.chunks = []chunk{a.storedInit}
  384. a.controlQueue.push(outbound)
  385. a.awakeWriteLoop()
  386. return nil
  387. }
  388. // caller must hold a.lock
  389. func (a *Association) sendCookieEcho() error {
  390. if a.storedCookieEcho == nil {
  391. return ErrCookieEchoNotStoredToSend
  392. }
  393. a.log.Debugf("[%s] sending COOKIE-ECHO", a.name)
  394. outbound := &packet{}
  395. outbound.verificationTag = a.peerVerificationTag
  396. outbound.sourcePort = a.sourcePort
  397. outbound.destinationPort = a.destinationPort
  398. outbound.chunks = []chunk{a.storedCookieEcho}
  399. a.controlQueue.push(outbound)
  400. a.awakeWriteLoop()
  401. return nil
  402. }
  403. // Shutdown initiates the shutdown sequence. The method blocks until the
  404. // shutdown sequence is completed and the connection is closed, or until the
  405. // passed context is done, in which case the context's error is returned.
  406. func (a *Association) Shutdown(ctx context.Context) error {
  407. a.log.Debugf("[%s] closing association..", a.name)
  408. state := a.getState()
  409. if state != established {
  410. return fmt.Errorf("%w: shutdown %s", ErrShutdownNonEstablished, a.name)
  411. }
  412. // Attempt a graceful shutdown.
  413. a.setState(shutdownPending)
  414. a.lock.Lock()
  415. if a.inflightQueue.size() == 0 {
  416. // No more outstanding, send shutdown.
  417. a.willSendShutdown = true
  418. a.awakeWriteLoop()
  419. a.setState(shutdownSent)
  420. }
  421. a.lock.Unlock()
  422. select {
  423. case <-a.closeWriteLoopCh:
  424. return nil
  425. case <-ctx.Done():
  426. return ctx.Err()
  427. }
  428. }
  429. // Close ends the SCTP Association and cleans up any state
  430. func (a *Association) Close() error {
  431. a.log.Debugf("[%s] closing association..", a.name)
  432. err := a.close()
  433. // Wait for readLoop to end
  434. <-a.readLoopCloseCh
  435. a.log.Debugf("[%s] association closed", a.name)
  436. a.log.Debugf("[%s] stats nPackets (in) : %d", a.name, a.stats.getNumPacketsReceived())
  437. a.log.Debugf("[%s] stats nPackets (out) : %d", a.name, a.stats.getNumPacketsSent())
  438. a.log.Debugf("[%s] stats nDATAs (in) : %d", a.name, a.stats.getNumDATAs())
  439. a.log.Debugf("[%s] stats nSACKs (in) : %d", a.name, a.stats.getNumSACKsReceived())
  440. a.log.Debugf("[%s] stats nSACKs (out) : %d\n", a.name, a.stats.getNumSACKsSent())
  441. a.log.Debugf("[%s] stats nT3Timeouts : %d", a.name, a.stats.getNumT3Timeouts())
  442. a.log.Debugf("[%s] stats nAckTimeouts: %d", a.name, a.stats.getNumAckTimeouts())
  443. a.log.Debugf("[%s] stats nFastRetrans: %d", a.name, a.stats.getNumFastRetrans())
  444. return err
  445. }
  446. func (a *Association) close() error {
  447. a.log.Debugf("[%s] closing association..", a.name)
  448. a.setState(closed)
  449. err := a.netConn.Close()
  450. a.closeAllTimers()
  451. // awake writeLoop to exit
  452. a.closeWriteLoopOnce.Do(func() { close(a.closeWriteLoopCh) })
  453. return err
  454. }
  455. // Abort sends the abort packet with user initiated abort and immediately
  456. // closes the connection.
  457. func (a *Association) Abort(reason string) {
  458. a.log.Debugf("[%s] aborting association: %s", a.name, reason)
  459. a.lock.Lock()
  460. a.willSendAbort = true
  461. a.willSendAbortCause = &errorCauseUserInitiatedAbort{
  462. upperLayerAbortReason: []byte(reason),
  463. }
  464. a.lock.Unlock()
  465. a.awakeWriteLoop()
  466. // Wait for readLoop to end
  467. <-a.readLoopCloseCh
  468. }
  469. func (a *Association) closeAllTimers() {
  470. // Close all retransmission & ack timers
  471. a.t1Init.close()
  472. a.t1Cookie.close()
  473. a.t2Shutdown.close()
  474. a.t3RTX.close()
  475. a.tReconfig.close()
  476. a.ackTimer.close()
  477. }
  478. func (a *Association) readLoop() {
  479. var closeErr error
  480. defer func() {
  481. // also stop writeLoop, otherwise writeLoop can be leaked
  482. // if connection is lost when there is no writing packet.
  483. a.closeWriteLoopOnce.Do(func() { close(a.closeWriteLoopCh) })
  484. a.lock.Lock()
  485. for _, s := range a.streams {
  486. a.unregisterStream(s, closeErr)
  487. }
  488. a.lock.Unlock()
  489. close(a.acceptCh)
  490. close(a.readLoopCloseCh)
  491. a.log.Debugf("[%s] association closed", a.name)
  492. a.log.Debugf("[%s] stats nDATAs (in) : %d", a.name, a.stats.getNumDATAs())
  493. a.log.Debugf("[%s] stats nSACKs (in) : %d", a.name, a.stats.getNumSACKsReceived())
  494. a.log.Debugf("[%s] stats nT3Timeouts : %d", a.name, a.stats.getNumT3Timeouts())
  495. a.log.Debugf("[%s] stats nAckTimeouts: %d", a.name, a.stats.getNumAckTimeouts())
  496. a.log.Debugf("[%s] stats nFastRetrans: %d", a.name, a.stats.getNumFastRetrans())
  497. }()
  498. a.log.Debugf("[%s] readLoop entered", a.name)
  499. buffer := make([]byte, receiveMTU)
  500. for {
  501. n, err := a.netConn.Read(buffer)
  502. if err != nil {
  503. closeErr = err
  504. break
  505. }
  506. // Make a buffer sized to what we read, then copy the data we
  507. // read from the underlying transport. We do this because the
  508. // user data is passed to the reassembly queue without
  509. // copying.
  510. inbound := make([]byte, n)
  511. copy(inbound, buffer[:n])
  512. atomic.AddUint64(&a.bytesReceived, uint64(n))
  513. if err = a.handleInbound(inbound); err != nil {
  514. closeErr = err
  515. break
  516. }
  517. }
  518. a.log.Debugf("[%s] readLoop exited %s", a.name, closeErr)
  519. }
  520. func (a *Association) writeLoop() {
  521. a.log.Debugf("[%s] writeLoop entered", a.name)
  522. defer a.log.Debugf("[%s] writeLoop exited", a.name)
  523. loop:
  524. for {
  525. rawPackets, ok := a.gatherOutbound()
  526. for _, raw := range rawPackets {
  527. _, err := a.netConn.Write(raw)
  528. if err != nil {
  529. if !errors.Is(err, io.EOF) {
  530. a.log.Warnf("[%s] failed to write packets on netConn: %v", a.name, err)
  531. }
  532. a.log.Debugf("[%s] writeLoop ended", a.name)
  533. break loop
  534. }
  535. atomic.AddUint64(&a.bytesSent, uint64(len(raw)))
  536. a.stats.incPacketsSent()
  537. }
  538. if !ok {
  539. if err := a.close(); err != nil {
  540. a.log.Warnf("[%s] failed to close association: %v", a.name, err)
  541. }
  542. return
  543. }
  544. select {
  545. case <-a.awakeWriteLoopCh:
  546. case <-a.closeWriteLoopCh:
  547. break loop
  548. }
  549. }
  550. a.setState(closed)
  551. a.closeAllTimers()
  552. }
  553. func (a *Association) awakeWriteLoop() {
  554. select {
  555. case a.awakeWriteLoopCh <- struct{}{}:
  556. default:
  557. }
  558. }
  559. // unregisterStream un-registers a stream from the association
  560. // The caller should hold the association write lock.
  561. func (a *Association) unregisterStream(s *Stream, err error) {
  562. s.lock.Lock()
  563. defer s.lock.Unlock()
  564. delete(a.streams, s.streamIdentifier)
  565. s.readErr = err
  566. s.readNotifier.Broadcast()
  567. }
  568. func chunkMandatoryChecksum(cc []chunk) bool {
  569. for _, c := range cc {
  570. switch c.(type) {
  571. case *chunkInit, *chunkCookieEcho:
  572. return true
  573. }
  574. }
  575. return false
  576. }
  577. func (a *Association) marshalPacket(p *packet) ([]byte, error) {
  578. return p.marshal(!a.sendZeroChecksum || chunkMandatoryChecksum(p.chunks))
  579. }
  580. func (a *Association) unmarshalPacket(raw []byte) (*packet, error) {
  581. p := &packet{}
  582. if err := p.unmarshal(!a.recvZeroChecksum, raw); err != nil {
  583. return nil, err
  584. }
  585. return p, nil
  586. }
  587. // handleInbound parses incoming raw packets
  588. func (a *Association) handleInbound(raw []byte) error {
  589. p, err := a.unmarshalPacket(raw)
  590. if err != nil {
  591. a.log.Warnf("[%s] unable to parse SCTP packet %s", a.name, err)
  592. return nil
  593. }
  594. if err := checkPacket(p); err != nil {
  595. a.log.Warnf("[%s] failed validating packet %s", a.name, err)
  596. return nil
  597. }
  598. a.handleChunksStart()
  599. for _, c := range p.chunks {
  600. if err := a.handleChunk(p, c); err != nil {
  601. return err
  602. }
  603. }
  604. a.handleChunksEnd()
  605. return nil
  606. }
  607. // The caller should hold the lock
  608. func (a *Association) gatherDataPacketsToRetransmit(rawPackets [][]byte) [][]byte {
  609. for _, p := range a.getDataPacketsToRetransmit() {
  610. raw, err := a.marshalPacket(p)
  611. if err != nil {
  612. a.log.Warnf("[%s] failed to serialize a DATA packet to be retransmitted", a.name)
  613. continue
  614. }
  615. rawPackets = append(rawPackets, raw)
  616. }
  617. return rawPackets
  618. }
  619. // The caller should hold the lock
  620. func (a *Association) gatherOutboundDataAndReconfigPackets(rawPackets [][]byte) [][]byte {
  621. // Pop unsent data chunks from the pending queue to send as much as
  622. // cwnd and rwnd allow.
  623. chunks, sisToReset := a.popPendingDataChunksToSend()
  624. if len(chunks) > 0 {
  625. // Start timer. (noop if already started)
  626. a.log.Tracef("[%s] T3-rtx timer start (pt1)", a.name)
  627. a.t3RTX.start(a.rtoMgr.getRTO())
  628. for _, p := range a.bundleDataChunksIntoPackets(chunks) {
  629. raw, err := a.marshalPacket(p)
  630. if err != nil {
  631. a.log.Warnf("[%s] failed to serialize a DATA packet", a.name)
  632. continue
  633. }
  634. rawPackets = append(rawPackets, raw)
  635. }
  636. }
  637. if len(sisToReset) > 0 || a.willRetransmitReconfig {
  638. if a.willRetransmitReconfig {
  639. a.willRetransmitReconfig = false
  640. a.log.Debugf("[%s] retransmit %d RECONFIG chunk(s)", a.name, len(a.reconfigs))
  641. for _, c := range a.reconfigs {
  642. p := a.createPacket([]chunk{c})
  643. raw, err := a.marshalPacket(p)
  644. if err != nil {
  645. a.log.Warnf("[%s] failed to serialize a RECONFIG packet to be retransmitted", a.name)
  646. } else {
  647. rawPackets = append(rawPackets, raw)
  648. }
  649. }
  650. }
  651. if len(sisToReset) > 0 {
  652. rsn := a.generateNextRSN()
  653. tsn := a.myNextTSN - 1
  654. c := &chunkReconfig{
  655. paramA: &paramOutgoingResetRequest{
  656. reconfigRequestSequenceNumber: rsn,
  657. senderLastTSN: tsn,
  658. streamIdentifiers: sisToReset,
  659. },
  660. }
  661. a.reconfigs[rsn] = c // store in the map for retransmission
  662. a.log.Debugf("[%s] sending RECONFIG: rsn=%d tsn=%d streams=%v",
  663. a.name, rsn, a.myNextTSN-1, sisToReset)
  664. p := a.createPacket([]chunk{c})
  665. raw, err := a.marshalPacket(p)
  666. if err != nil {
  667. a.log.Warnf("[%s] failed to serialize a RECONFIG packet to be transmitted", a.name)
  668. } else {
  669. rawPackets = append(rawPackets, raw)
  670. }
  671. }
  672. if len(a.reconfigs) > 0 {
  673. a.tReconfig.start(a.rtoMgr.getRTO())
  674. }
  675. }
  676. return rawPackets
  677. }
  678. // The caller should hold the lock
  679. func (a *Association) gatherOutboundFastRetransmissionPackets(rawPackets [][]byte) [][]byte {
  680. if a.willRetransmitFast {
  681. a.willRetransmitFast = false
  682. toFastRetrans := []chunk{}
  683. fastRetransSize := commonHeaderSize
  684. for i := 0; ; i++ {
  685. c, ok := a.inflightQueue.get(a.cumulativeTSNAckPoint + uint32(i) + 1)
  686. if !ok {
  687. break // end of pending data
  688. }
  689. if c.acked || c.abandoned() {
  690. continue
  691. }
  692. if c.nSent > 1 || c.missIndicator < 3 {
  693. continue
  694. }
  695. // RFC 4960 Sec 7.2.4 Fast Retransmit on Gap Reports
  696. // 3) Determine how many of the earliest (i.e., lowest TSN) DATA chunks
  697. // marked for retransmission will fit into a single packet, subject
  698. // to constraint of the path MTU of the destination transport
  699. // address to which the packet is being sent. Call this value K.
  700. // Retransmit those K DATA chunks in a single packet. When a Fast
  701. // Retransmit is being performed, the sender SHOULD ignore the value
  702. // of cwnd and SHOULD NOT delay retransmission for this single
  703. // packet.
  704. dataChunkSize := dataChunkHeaderSize + uint32(len(c.userData))
  705. if a.MTU() < fastRetransSize+dataChunkSize {
  706. break
  707. }
  708. fastRetransSize += dataChunkSize
  709. a.stats.incFastRetrans()
  710. c.nSent++
  711. a.checkPartialReliabilityStatus(c)
  712. toFastRetrans = append(toFastRetrans, c)
  713. a.log.Tracef("[%s] fast-retransmit: tsn=%d sent=%d htna=%d",
  714. a.name, c.tsn, c.nSent, a.fastRecoverExitPoint)
  715. }
  716. if len(toFastRetrans) > 0 {
  717. raw, err := a.marshalPacket(a.createPacket(toFastRetrans))
  718. if err != nil {
  719. a.log.Warnf("[%s] failed to serialize a DATA packet to be fast-retransmitted", a.name)
  720. } else {
  721. rawPackets = append(rawPackets, raw)
  722. }
  723. }
  724. }
  725. return rawPackets
  726. }
  727. // The caller should hold the lock
  728. func (a *Association) gatherOutboundSackPackets(rawPackets [][]byte) [][]byte {
  729. if a.ackState == ackStateImmediate {
  730. a.ackState = ackStateIdle
  731. sack := a.createSelectiveAckChunk()
  732. a.stats.incSACKsSent()
  733. a.log.Debugf("[%s] sending SACK: %s", a.name, sack)
  734. raw, err := a.marshalPacket(a.createPacket([]chunk{sack}))
  735. if err != nil {
  736. a.log.Warnf("[%s] failed to serialize a SACK packet", a.name)
  737. } else {
  738. rawPackets = append(rawPackets, raw)
  739. }
  740. }
  741. return rawPackets
  742. }
  743. // The caller should hold the lock
  744. func (a *Association) gatherOutboundForwardTSNPackets(rawPackets [][]byte) [][]byte {
  745. if a.willSendForwardTSN {
  746. a.willSendForwardTSN = false
  747. if sna32GT(a.advancedPeerTSNAckPoint, a.cumulativeTSNAckPoint) {
  748. fwdtsn := a.createForwardTSN()
  749. raw, err := a.marshalPacket(a.createPacket([]chunk{fwdtsn}))
  750. if err != nil {
  751. a.log.Warnf("[%s] failed to serialize a Forward TSN packet", a.name)
  752. } else {
  753. rawPackets = append(rawPackets, raw)
  754. }
  755. }
  756. }
  757. return rawPackets
  758. }
  759. func (a *Association) gatherOutboundShutdownPackets(rawPackets [][]byte) ([][]byte, bool) {
  760. ok := true
  761. switch {
  762. case a.willSendShutdown:
  763. a.willSendShutdown = false
  764. shutdown := &chunkShutdown{
  765. cumulativeTSNAck: a.cumulativeTSNAckPoint,
  766. }
  767. raw, err := a.marshalPacket(a.createPacket([]chunk{shutdown}))
  768. if err != nil {
  769. a.log.Warnf("[%s] failed to serialize a Shutdown packet", a.name)
  770. } else {
  771. a.t2Shutdown.start(a.rtoMgr.getRTO())
  772. rawPackets = append(rawPackets, raw)
  773. }
  774. case a.willSendShutdownAck:
  775. a.willSendShutdownAck = false
  776. shutdownAck := &chunkShutdownAck{}
  777. raw, err := a.marshalPacket(a.createPacket([]chunk{shutdownAck}))
  778. if err != nil {
  779. a.log.Warnf("[%s] failed to serialize a ShutdownAck packet", a.name)
  780. } else {
  781. a.t2Shutdown.start(a.rtoMgr.getRTO())
  782. rawPackets = append(rawPackets, raw)
  783. }
  784. case a.willSendShutdownComplete:
  785. a.willSendShutdownComplete = false
  786. shutdownComplete := &chunkShutdownComplete{}
  787. raw, err := a.marshalPacket(a.createPacket([]chunk{shutdownComplete}))
  788. if err != nil {
  789. a.log.Warnf("[%s] failed to serialize a ShutdownComplete packet", a.name)
  790. } else {
  791. rawPackets = append(rawPackets, raw)
  792. ok = false
  793. }
  794. }
  795. return rawPackets, ok
  796. }
  797. func (a *Association) gatherAbortPacket() ([]byte, error) {
  798. cause := a.willSendAbortCause
  799. a.willSendAbort = false
  800. a.willSendAbortCause = nil
  801. abort := &chunkAbort{}
  802. if cause != nil {
  803. abort.errorCauses = []errorCause{cause}
  804. }
  805. raw, err := a.marshalPacket(a.createPacket([]chunk{abort}))
  806. return raw, err
  807. }
  808. // gatherOutbound gathers outgoing packets. The returned bool value set to
  809. // false means the association should be closed down after the final send.
  810. func (a *Association) gatherOutbound() ([][]byte, bool) {
  811. a.lock.Lock()
  812. defer a.lock.Unlock()
  813. if a.willSendAbort {
  814. pkt, err := a.gatherAbortPacket()
  815. if err != nil {
  816. a.log.Warnf("[%s] failed to serialize an abort packet", a.name)
  817. return nil, false
  818. }
  819. return [][]byte{pkt}, false
  820. }
  821. rawPackets := [][]byte{}
  822. if a.controlQueue.size() > 0 {
  823. for _, p := range a.controlQueue.popAll() {
  824. raw, err := a.marshalPacket(p)
  825. if err != nil {
  826. a.log.Warnf("[%s] failed to serialize a control packet", a.name)
  827. continue
  828. }
  829. rawPackets = append(rawPackets, raw)
  830. }
  831. }
  832. state := a.getState()
  833. ok := true
  834. switch state {
  835. case established:
  836. rawPackets = a.gatherDataPacketsToRetransmit(rawPackets)
  837. rawPackets = a.gatherOutboundDataAndReconfigPackets(rawPackets)
  838. rawPackets = a.gatherOutboundFastRetransmissionPackets(rawPackets)
  839. rawPackets = a.gatherOutboundSackPackets(rawPackets)
  840. rawPackets = a.gatherOutboundForwardTSNPackets(rawPackets)
  841. case shutdownPending, shutdownSent, shutdownReceived:
  842. rawPackets = a.gatherDataPacketsToRetransmit(rawPackets)
  843. rawPackets = a.gatherOutboundFastRetransmissionPackets(rawPackets)
  844. rawPackets = a.gatherOutboundSackPackets(rawPackets)
  845. rawPackets, ok = a.gatherOutboundShutdownPackets(rawPackets)
  846. case shutdownAckSent:
  847. rawPackets, ok = a.gatherOutboundShutdownPackets(rawPackets)
  848. }
  849. return rawPackets, ok
  850. }
  851. func checkPacket(p *packet) error {
  852. // All packets must adhere to these rules
  853. // This is the SCTP sender's port number. It can be used by the
  854. // receiver in combination with the source IP address, the SCTP
  855. // destination port, and possibly the destination IP address to
  856. // identify the association to which this packet belongs. The port
  857. // number 0 MUST NOT be used.
  858. if p.sourcePort == 0 {
  859. return ErrSCTPPacketSourcePortZero
  860. }
  861. // This is the SCTP port number to which this packet is destined.
  862. // The receiving host will use this port number to de-multiplex the
  863. // SCTP packet to the correct receiving endpoint/application. The
  864. // port number 0 MUST NOT be used.
  865. if p.destinationPort == 0 {
  866. return ErrSCTPPacketDestinationPortZero
  867. }
  868. // Check values on the packet that are specific to a particular chunk type
  869. for _, c := range p.chunks {
  870. switch c.(type) { // nolint:gocritic
  871. case *chunkInit:
  872. // An INIT or INIT ACK chunk MUST NOT be bundled with any other chunk.
  873. // They MUST be the only chunks present in the SCTP packets that carry
  874. // them.
  875. if len(p.chunks) != 1 {
  876. return ErrInitChunkBundled
  877. }
  878. // A packet containing an INIT chunk MUST have a zero Verification
  879. // Tag.
  880. if p.verificationTag != 0 {
  881. return ErrInitChunkVerifyTagNotZero
  882. }
  883. }
  884. }
  885. return nil
  886. }
  887. func min16(a, b uint16) uint16 {
  888. if a < b {
  889. return a
  890. }
  891. return b
  892. }
  893. func max32(a, b uint32) uint32 {
  894. if a > b {
  895. return a
  896. }
  897. return b
  898. }
  899. func min32(a, b uint32) uint32 {
  900. if a < b {
  901. return a
  902. }
  903. return b
  904. }
  905. // setState atomically sets the state of the Association.
  906. // The caller should hold the lock.
  907. func (a *Association) setState(newState uint32) {
  908. oldState := atomic.SwapUint32(&a.state, newState)
  909. if newState != oldState {
  910. a.log.Debugf("[%s] state change: '%s' => '%s'",
  911. a.name,
  912. getAssociationStateString(oldState),
  913. getAssociationStateString(newState))
  914. }
  915. }
  916. // getState atomically returns the state of the Association.
  917. func (a *Association) getState() uint32 {
  918. return atomic.LoadUint32(&a.state)
  919. }
  920. // BytesSent returns the number of bytes sent
  921. func (a *Association) BytesSent() uint64 {
  922. return atomic.LoadUint64(&a.bytesSent)
  923. }
  924. // BytesReceived returns the number of bytes received
  925. func (a *Association) BytesReceived() uint64 {
  926. return atomic.LoadUint64(&a.bytesReceived)
  927. }
  928. // MTU returns the association's current MTU
  929. func (a *Association) MTU() uint32 {
  930. return atomic.LoadUint32(&a.mtu)
  931. }
  932. // CWND returns the association's current congestion window (cwnd)
  933. func (a *Association) CWND() uint32 {
  934. return atomic.LoadUint32(&a.cwnd)
  935. }
  936. func (a *Association) setCWND(cwnd uint32) {
  937. atomic.StoreUint32(&a.cwnd, cwnd)
  938. }
  939. // RWND returns the association's current receiver window (rwnd)
  940. func (a *Association) RWND() uint32 {
  941. return atomic.LoadUint32(&a.rwnd)
  942. }
  943. func (a *Association) setRWND(rwnd uint32) {
  944. atomic.StoreUint32(&a.rwnd, rwnd)
  945. }
  946. // SRTT returns the latest smoothed round-trip time (srrt)
  947. func (a *Association) SRTT() float64 {
  948. return a.srtt.Load().(float64) //nolint:forcetypeassert
  949. }
  950. // getMaxTSNOffset returns the maximum offset over the current cummulative TSN that
  951. // we are willing to enqueue. Limiting the maximum offset limits the number of
  952. // tsns we have in the payloadQueue map. This ensures that we don't use too much space in
  953. // the map itself. This also ensures that we keep the bytes utilized in the receive
  954. // buffer within a small multiple of the user provided max receive buffer size.
  955. func (a *Association) getMaxTSNOffset() uint32 {
  956. // 4 is a magic number here. There is no theory behind this.
  957. offset := (a.maxReceiveBufferSize * 4) / avgChunkSize
  958. if offset < minTSNOffset {
  959. offset = minTSNOffset
  960. }
  961. if offset > maxTSNOffset {
  962. offset = maxTSNOffset
  963. }
  964. return offset
  965. }
  966. func setSupportedExtensions(init *chunkInitCommon) {
  967. // nolint:godox
  968. // TODO RFC5061 https://tools.ietf.org/html/rfc6525#section-5.2
  969. // An implementation supporting this (Supported Extensions Parameter)
  970. // extension MUST list the ASCONF, the ASCONF-ACK, and the AUTH chunks
  971. // in its INIT and INIT-ACK parameters.
  972. init.params = append(init.params, &paramSupportedExtensions{
  973. ChunkTypes: []chunkType{ctReconfig, ctForwardTSN},
  974. })
  975. }
  976. // The caller should hold the lock.
  977. func (a *Association) handleInit(p *packet, i *chunkInit) ([]*packet, error) {
  978. state := a.getState()
  979. a.log.Debugf("[%s] chunkInit received in state '%s'", a.name, getAssociationStateString(state))
  980. // https://tools.ietf.org/html/rfc4960#section-5.2.1
  981. // Upon receipt of an INIT in the COOKIE-WAIT state, an endpoint MUST
  982. // respond with an INIT ACK using the same parameters it sent in its
  983. // original INIT chunk (including its Initiate Tag, unchanged). When
  984. // responding, the endpoint MUST send the INIT ACK back to the same
  985. // address that the original INIT (sent by this endpoint) was sent.
  986. if state != closed && state != cookieWait && state != cookieEchoed {
  987. // 5.2.2. Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
  988. // COOKIE-WAIT, and SHUTDOWN-ACK-SENT
  989. return nil, fmt.Errorf("%w: %s", ErrHandleInitState, getAssociationStateString(state))
  990. }
  991. // NOTE: Setting these prior to a reception of a COOKIE ECHO chunk containing
  992. // our cookie is not compliant with https://www.rfc-editor.org/rfc/rfc9260#section-5.1-2.2.3.
  993. // It makes us more vulnerable to resource attacks, albeit minimally so.
  994. // https://www.rfc-editor.org/rfc/rfc9260#sec_handle_stream_parameters
  995. a.myMaxNumInboundStreams = min16(i.numInboundStreams, a.myMaxNumInboundStreams)
  996. a.myMaxNumOutboundStreams = min16(i.numOutboundStreams, a.myMaxNumOutboundStreams)
  997. a.peerVerificationTag = i.initiateTag
  998. a.sourcePort = p.destinationPort
  999. a.destinationPort = p.sourcePort
  1000. // 13.2 This is the last TSN received in sequence. This value
  1001. // is set initially by taking the peer's initial TSN,
  1002. // received in the INIT or INIT ACK chunk, and
  1003. // subtracting one from it.
  1004. a.peerLastTSN = i.initialTSN - 1
  1005. for _, param := range i.params {
  1006. switch v := param.(type) { // nolint:gocritic
  1007. case *paramSupportedExtensions:
  1008. for _, t := range v.ChunkTypes {
  1009. if t == ctForwardTSN {
  1010. a.log.Debugf("[%s] use ForwardTSN (on init)", a.name)
  1011. a.useForwardTSN = true
  1012. }
  1013. }
  1014. case *paramZeroChecksumAcceptable:
  1015. a.sendZeroChecksum = v.edmid == dtlsErrorDetectionMethod
  1016. }
  1017. }
  1018. if !a.useForwardTSN {
  1019. a.log.Warnf("[%s] not using ForwardTSN (on init)", a.name)
  1020. }
  1021. outbound := &packet{}
  1022. outbound.verificationTag = a.peerVerificationTag
  1023. outbound.sourcePort = a.sourcePort
  1024. outbound.destinationPort = a.destinationPort
  1025. initAck := &chunkInitAck{}
  1026. a.log.Debug("sending INIT ACK")
  1027. initAck.initialTSN = a.myNextTSN
  1028. initAck.numOutboundStreams = a.myMaxNumOutboundStreams
  1029. initAck.numInboundStreams = a.myMaxNumInboundStreams
  1030. initAck.initiateTag = a.myVerificationTag
  1031. initAck.advertisedReceiverWindowCredit = a.maxReceiveBufferSize
  1032. if a.myCookie == nil {
  1033. var err error
  1034. // NOTE: This generation process is not compliant with
  1035. // 5.1.3. Generating State Cookie (https://www.rfc-editor.org/rfc/rfc4960#section-5.1.3)
  1036. if a.myCookie, err = newRandomStateCookie(); err != nil {
  1037. return nil, err
  1038. }
  1039. }
  1040. initAck.params = []param{a.myCookie}
  1041. if a.recvZeroChecksum {
  1042. initAck.params = append(initAck.params, &paramZeroChecksumAcceptable{edmid: dtlsErrorDetectionMethod})
  1043. }
  1044. a.log.Debugf("[%s] sendZeroChecksum=%t (on init)", a.name, a.sendZeroChecksum)
  1045. setSupportedExtensions(&initAck.chunkInitCommon)
  1046. outbound.chunks = []chunk{initAck}
  1047. return pack(outbound), nil
  1048. }
  1049. // The caller should hold the lock.
  1050. func (a *Association) handleInitAck(p *packet, i *chunkInitAck) error {
  1051. state := a.getState()
  1052. a.log.Debugf("[%s] chunkInitAck received in state '%s'", a.name, getAssociationStateString(state))
  1053. if state != cookieWait {
  1054. // RFC 4960
  1055. // 5.2.3. Unexpected INIT ACK
  1056. // If an INIT ACK is received by an endpoint in any state other than the
  1057. // COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
  1058. // An unexpected INIT ACK usually indicates the processing of an old or
  1059. // duplicated INIT chunk.
  1060. return nil
  1061. }
  1062. a.myMaxNumInboundStreams = min16(i.numInboundStreams, a.myMaxNumInboundStreams)
  1063. a.myMaxNumOutboundStreams = min16(i.numOutboundStreams, a.myMaxNumOutboundStreams)
  1064. a.peerVerificationTag = i.initiateTag
  1065. a.peerLastTSN = i.initialTSN - 1
  1066. if a.sourcePort != p.destinationPort ||
  1067. a.destinationPort != p.sourcePort {
  1068. a.log.Warnf("[%s] handleInitAck: port mismatch", a.name)
  1069. return nil
  1070. }
  1071. a.setRWND(i.advertisedReceiverWindowCredit)
  1072. a.log.Debugf("[%s] initial rwnd=%d", a.name, a.RWND())
  1073. // RFC 4690 Sec 7.2.1
  1074. // o The initial value of ssthresh MAY be arbitrarily high (for
  1075. // example, implementations MAY use the size of the receiver
  1076. // advertised window).
  1077. a.ssthresh = a.RWND()
  1078. a.log.Tracef("[%s] updated cwnd=%d ssthresh=%d inflight=%d (INI)",
  1079. a.name, a.CWND(), a.ssthresh, a.inflightQueue.getNumBytes())
  1080. a.t1Init.stop()
  1081. a.storedInit = nil
  1082. var cookieParam *paramStateCookie
  1083. for _, param := range i.params {
  1084. switch v := param.(type) {
  1085. case *paramStateCookie:
  1086. cookieParam = v
  1087. case *paramSupportedExtensions:
  1088. for _, t := range v.ChunkTypes {
  1089. if t == ctForwardTSN {
  1090. a.log.Debugf("[%s] use ForwardTSN (on initAck)", a.name)
  1091. a.useForwardTSN = true
  1092. }
  1093. }
  1094. case *paramZeroChecksumAcceptable:
  1095. a.sendZeroChecksum = v.edmid == dtlsErrorDetectionMethod
  1096. }
  1097. }
  1098. a.log.Debugf("[%s] sendZeroChecksum=%t (on initAck)", a.name, a.sendZeroChecksum)
  1099. if !a.useForwardTSN {
  1100. a.log.Warnf("[%s] not using ForwardTSN (on initAck)", a.name)
  1101. }
  1102. if cookieParam == nil {
  1103. return ErrInitAckNoCookie
  1104. }
  1105. a.storedCookieEcho = &chunkCookieEcho{}
  1106. a.storedCookieEcho.cookie = cookieParam.cookie
  1107. err := a.sendCookieEcho()
  1108. if err != nil {
  1109. a.log.Errorf("[%s] failed to send init: %s", a.name, err.Error())
  1110. }
  1111. a.t1Cookie.start(a.rtoMgr.getRTO())
  1112. a.setState(cookieEchoed)
  1113. return nil
  1114. }
  1115. // The caller should hold the lock.
  1116. func (a *Association) handleHeartbeat(c *chunkHeartbeat) []*packet {
  1117. a.log.Tracef("[%s] chunkHeartbeat", a.name)
  1118. hbi, ok := c.params[0].(*paramHeartbeatInfo)
  1119. if !ok {
  1120. a.log.Warnf("[%s] failed to handle Heartbeat, no ParamHeartbeatInfo", a.name)
  1121. }
  1122. return pack(&packet{
  1123. verificationTag: a.peerVerificationTag,
  1124. sourcePort: a.sourcePort,
  1125. destinationPort: a.destinationPort,
  1126. chunks: []chunk{&chunkHeartbeatAck{
  1127. params: []param{
  1128. &paramHeartbeatInfo{
  1129. heartbeatInformation: hbi.heartbeatInformation,
  1130. },
  1131. },
  1132. }},
  1133. })
  1134. }
  1135. // The caller should hold the lock.
  1136. func (a *Association) handleCookieEcho(c *chunkCookieEcho) []*packet {
  1137. state := a.getState()
  1138. a.log.Debugf("[%s] COOKIE-ECHO received in state '%s'", a.name, getAssociationStateString(state))
  1139. if a.myCookie == nil {
  1140. a.log.Debugf("[%s] COOKIE-ECHO received before initialization", a.name)
  1141. return nil
  1142. }
  1143. switch state {
  1144. default:
  1145. return nil
  1146. case established:
  1147. if !bytes.Equal(a.myCookie.cookie, c.cookie) {
  1148. return nil
  1149. }
  1150. case closed, cookieWait, cookieEchoed:
  1151. if !bytes.Equal(a.myCookie.cookie, c.cookie) {
  1152. return nil
  1153. }
  1154. // RFC wise, these do not seem to belong here, but removing them
  1155. // causes TestCookieEchoRetransmission to break
  1156. a.t1Init.stop()
  1157. a.storedInit = nil
  1158. a.t1Cookie.stop()
  1159. a.storedCookieEcho = nil
  1160. a.setState(established)
  1161. // Note: This is a future place where the user could be notified (COMMUNICATION UP)
  1162. a.handshakeCompletedCh <- nil
  1163. }
  1164. p := &packet{
  1165. verificationTag: a.peerVerificationTag,
  1166. sourcePort: a.sourcePort,
  1167. destinationPort: a.destinationPort,
  1168. chunks: []chunk{&chunkCookieAck{}},
  1169. }
  1170. return pack(p)
  1171. }
  1172. // The caller should hold the lock.
  1173. func (a *Association) handleCookieAck() {
  1174. state := a.getState()
  1175. a.log.Debugf("[%s] COOKIE-ACK received in state '%s'", a.name, getAssociationStateString(state))
  1176. if state != cookieEchoed {
  1177. // RFC 4960
  1178. // 5.2.5. Handle Duplicate COOKIE-ACK.
  1179. // At any state other than COOKIE-ECHOED, an endpoint should silently
  1180. // discard a received COOKIE ACK chunk.
  1181. return
  1182. }
  1183. a.t1Cookie.stop()
  1184. a.storedCookieEcho = nil
  1185. a.setState(established)
  1186. // Note: This is a future place where the user could be notified (COMMUNICATION UP)
  1187. a.handshakeCompletedCh <- nil
  1188. }
  1189. // The caller should hold the lock.
  1190. func (a *Association) handleData(d *chunkPayloadData) []*packet {
  1191. a.log.Tracef("[%s] DATA: tsn=%d immediateSack=%v len=%d",
  1192. a.name, d.tsn, d.immediateSack, len(d.userData))
  1193. a.stats.incDATAs()
  1194. canPush := a.payloadQueue.canPush(d, a.peerLastTSN, a.getMaxTSNOffset())
  1195. if canPush {
  1196. s := a.getOrCreateStream(d.streamIdentifier, true, PayloadTypeUnknown)
  1197. if s == nil {
  1198. // silently discard the data. (sender will retry on T3-rtx timeout)
  1199. // see pion/sctp#30
  1200. a.log.Debugf("[%s] discard %d", a.name, d.streamSequenceNumber)
  1201. return nil
  1202. }
  1203. if a.getMyReceiverWindowCredit() > 0 {
  1204. // Pass the new chunk to stream level as soon as it arrives
  1205. a.payloadQueue.push(d, a.peerLastTSN)
  1206. s.handleData(d)
  1207. } else {
  1208. // Receive buffer is full
  1209. lastTSN, ok := a.payloadQueue.getLastTSNReceived()
  1210. if ok && sna32LT(d.tsn, lastTSN) {
  1211. a.log.Debugf("[%s] receive buffer full, but accepted as this is a missing chunk with tsn=%d ssn=%d", a.name, d.tsn, d.streamSequenceNumber)
  1212. a.payloadQueue.push(d, a.peerLastTSN)
  1213. s.handleData(d)
  1214. } else {
  1215. a.log.Debugf("[%s] receive buffer full. dropping DATA with tsn=%d ssn=%d", a.name, d.tsn, d.streamSequenceNumber)
  1216. }
  1217. }
  1218. }
  1219. return a.handlePeerLastTSNAndAcknowledgement(d.immediateSack)
  1220. }
  1221. // A common routine for handleData and handleForwardTSN routines
  1222. // The caller should hold the lock.
  1223. func (a *Association) handlePeerLastTSNAndAcknowledgement(sackImmediately bool) []*packet {
  1224. var reply []*packet
  1225. // Try to advance peerLastTSN
  1226. // From RFC 3758 Sec 3.6:
  1227. // .. and then MUST further advance its cumulative TSN point locally
  1228. // if possible
  1229. // Meaning, if peerLastTSN+1 points to a chunk that is received,
  1230. // advance peerLastTSN until peerLastTSN+1 points to unreceived chunk.
  1231. for {
  1232. if _, popOk := a.payloadQueue.pop(a.peerLastTSN + 1); !popOk {
  1233. break
  1234. }
  1235. a.peerLastTSN++
  1236. for _, rstReq := range a.reconfigRequests {
  1237. resp := a.resetStreamsIfAny(rstReq)
  1238. if resp != nil {
  1239. a.log.Debugf("[%s] RESET RESPONSE: %+v", a.name, resp)
  1240. reply = append(reply, resp)
  1241. }
  1242. }
  1243. }
  1244. hasPacketLoss := (a.payloadQueue.size() > 0)
  1245. if hasPacketLoss {
  1246. a.log.Tracef("[%s] packetloss: %s", a.name, a.payloadQueue.getGapAckBlocksString(a.peerLastTSN))
  1247. }
  1248. if (a.ackState != ackStateImmediate && !sackImmediately && !hasPacketLoss && a.ackMode == ackModeNormal) || a.ackMode == ackModeAlwaysDelay {
  1249. if a.ackState == ackStateIdle {
  1250. a.delayedAckTriggered = true
  1251. } else {
  1252. a.immediateAckTriggered = true
  1253. }
  1254. } else {
  1255. a.immediateAckTriggered = true
  1256. }
  1257. return reply
  1258. }
  1259. // The caller should hold the lock.
  1260. func (a *Association) getMyReceiverWindowCredit() uint32 {
  1261. var bytesQueued uint32
  1262. for _, s := range a.streams {
  1263. bytesQueued += uint32(s.getNumBytesInReassemblyQueue())
  1264. }
  1265. if bytesQueued >= a.maxReceiveBufferSize {
  1266. return 0
  1267. }
  1268. return a.maxReceiveBufferSize - bytesQueued
  1269. }
  1270. // OpenStream opens a stream
  1271. func (a *Association) OpenStream(streamIdentifier uint16, defaultPayloadType PayloadProtocolIdentifier) (*Stream, error) {
  1272. a.lock.Lock()
  1273. defer a.lock.Unlock()
  1274. return a.getOrCreateStream(streamIdentifier, false, defaultPayloadType), nil
  1275. }
  1276. // AcceptStream accepts a stream
  1277. func (a *Association) AcceptStream() (*Stream, error) {
  1278. s, ok := <-a.acceptCh
  1279. if !ok {
  1280. return nil, io.EOF // no more incoming streams
  1281. }
  1282. return s, nil
  1283. }
  1284. // createStream creates a stream. The caller should hold the lock and check no stream exists for this id.
  1285. func (a *Association) createStream(streamIdentifier uint16, accept bool) *Stream {
  1286. s := &Stream{
  1287. association: a,
  1288. streamIdentifier: streamIdentifier,
  1289. reassemblyQueue: newReassemblyQueue(streamIdentifier),
  1290. log: a.log,
  1291. name: fmt.Sprintf("%d:%s", streamIdentifier, a.name),
  1292. }
  1293. s.readNotifier = sync.NewCond(&s.lock)
  1294. if accept {
  1295. select {
  1296. case a.acceptCh <- s:
  1297. a.streams[streamIdentifier] = s
  1298. a.log.Debugf("[%s] accepted a new stream (streamIdentifier: %d)",
  1299. a.name, streamIdentifier)
  1300. default:
  1301. a.log.Debugf("[%s] dropped a new stream (acceptCh size: %d)",
  1302. a.name, len(a.acceptCh))
  1303. return nil
  1304. }
  1305. } else {
  1306. a.streams[streamIdentifier] = s
  1307. }
  1308. return s
  1309. }
  1310. // getOrCreateStream gets or creates a stream. The caller should hold the lock.
  1311. func (a *Association) getOrCreateStream(streamIdentifier uint16, accept bool, defaultPayloadType PayloadProtocolIdentifier) *Stream {
  1312. if s, ok := a.streams[streamIdentifier]; ok {
  1313. s.SetDefaultPayloadType(defaultPayloadType)
  1314. return s
  1315. }
  1316. s := a.createStream(streamIdentifier, accept)
  1317. if s != nil {
  1318. s.SetDefaultPayloadType(defaultPayloadType)
  1319. }
  1320. return s
  1321. }
  1322. // The caller should hold the lock.
  1323. func (a *Association) processSelectiveAck(d *chunkSelectiveAck) (map[uint16]int, uint32, error) { // nolint:gocognit
  1324. bytesAckedPerStream := map[uint16]int{}
  1325. // New ack point, so pop all ACKed packets from inflightQueue
  1326. // We add 1 because the "currentAckPoint" has already been popped from the inflight queue
  1327. // For the first SACK we take care of this by setting the ackpoint to cumAck - 1
  1328. for i := a.cumulativeTSNAckPoint + 1; sna32LTE(i, d.cumulativeTSNAck); i++ {
  1329. c, ok := a.inflightQueue.pop(i)
  1330. if !ok {
  1331. return nil, 0, fmt.Errorf("%w: %v", ErrInflightQueueTSNPop, i)
  1332. }
  1333. if !c.acked {
  1334. // RFC 4096 sec 6.3.2. Retransmission Timer Rules
  1335. // R3) Whenever a SACK is received that acknowledges the DATA chunk
  1336. // with the earliest outstanding TSN for that address, restart the
  1337. // T3-rtx timer for that address with its current RTO (if there is
  1338. // still outstanding data on that address).
  1339. if i == a.cumulativeTSNAckPoint+1 {
  1340. // T3 timer needs to be reset. Stop it for now.
  1341. a.t3RTX.stop()
  1342. }
  1343. nBytesAcked := len(c.userData)
  1344. // Sum the number of bytes acknowledged per stream
  1345. if amount, ok := bytesAckedPerStream[c.streamIdentifier]; ok {
  1346. bytesAckedPerStream[c.streamIdentifier] = amount + nBytesAcked
  1347. } else {
  1348. bytesAckedPerStream[c.streamIdentifier] = nBytesAcked
  1349. }
  1350. // RFC 4960 sec 6.3.1. RTO Calculation
  1351. // C4) When data is in flight and when allowed by rule C5 below, a new
  1352. // RTT measurement MUST be made each round trip. Furthermore, new
  1353. // RTT measurements SHOULD be made no more than once per round trip
  1354. // for a given destination transport address.
  1355. // C5) Karn's algorithm: RTT measurements MUST NOT be made using
  1356. // packets that were retransmitted (and thus for which it is
  1357. // ambiguous whether the reply was for the first instance of the
  1358. // chunk or for a later instance)
  1359. if c.nSent == 1 && sna32GTE(c.tsn, a.minTSN2MeasureRTT) {
  1360. a.minTSN2MeasureRTT = a.myNextTSN
  1361. rtt := time.Since(c.since).Seconds() * 1000.0
  1362. srtt := a.rtoMgr.setNewRTT(rtt)
  1363. a.srtt.Store(srtt)
  1364. a.log.Tracef("[%s] SACK: measured-rtt=%f srtt=%f new-rto=%f",
  1365. a.name, rtt, srtt, a.rtoMgr.getRTO())
  1366. }
  1367. }
  1368. if a.inFastRecovery && c.tsn == a.fastRecoverExitPoint {
  1369. a.log.Debugf("[%s] exit fast-recovery", a.name)
  1370. a.inFastRecovery = false
  1371. }
  1372. }
  1373. htna := d.cumulativeTSNAck
  1374. // Mark selectively acknowledged chunks as "acked"
  1375. for _, g := range d.gapAckBlocks {
  1376. for i := g.start; i <= g.end; i++ {
  1377. tsn := d.cumulativeTSNAck + uint32(i)
  1378. c, ok := a.inflightQueue.get(tsn)
  1379. if !ok {
  1380. return nil, 0, fmt.Errorf("%w: %v", ErrTSNRequestNotExist, tsn)
  1381. }
  1382. if !c.acked {
  1383. nBytesAcked := a.inflightQueue.markAsAcked(tsn)
  1384. // Sum the number of bytes acknowledged per stream
  1385. if amount, ok := bytesAckedPerStream[c.streamIdentifier]; ok {
  1386. bytesAckedPerStream[c.streamIdentifier] = amount + nBytesAcked
  1387. } else {
  1388. bytesAckedPerStream[c.streamIdentifier] = nBytesAcked
  1389. }
  1390. a.log.Tracef("[%s] tsn=%d has been sacked", a.name, c.tsn)
  1391. if c.nSent == 1 {
  1392. a.minTSN2MeasureRTT = a.myNextTSN
  1393. rtt := time.Since(c.since).Seconds() * 1000.0
  1394. srtt := a.rtoMgr.setNewRTT(rtt)
  1395. a.srtt.Store(srtt)
  1396. a.log.Tracef("[%s] SACK: measured-rtt=%f srtt=%f new-rto=%f",
  1397. a.name, rtt, srtt, a.rtoMgr.getRTO())
  1398. }
  1399. if sna32LT(htna, tsn) {
  1400. htna = tsn
  1401. }
  1402. }
  1403. }
  1404. }
  1405. return bytesAckedPerStream, htna, nil
  1406. }
  1407. // The caller should hold the lock.
  1408. func (a *Association) onCumulativeTSNAckPointAdvanced(totalBytesAcked int) {
  1409. // RFC 4096, sec 6.3.2. Retransmission Timer Rules
  1410. // R2) Whenever all outstanding data sent to an address have been
  1411. // acknowledged, turn off the T3-rtx timer of that address.
  1412. if a.inflightQueue.size() == 0 {
  1413. a.log.Tracef("[%s] SACK: no more packet in-flight (pending=%d)", a.name, a.pendingQueue.size())
  1414. a.t3RTX.stop()
  1415. } else {
  1416. a.log.Tracef("[%s] T3-rtx timer start (pt2)", a.name)
  1417. a.t3RTX.start(a.rtoMgr.getRTO())
  1418. }
  1419. // Update congestion control parameters
  1420. if a.CWND() <= a.ssthresh {
  1421. // RFC 4096, sec 7.2.1. Slow-Start
  1422. // o When cwnd is less than or equal to ssthresh, an SCTP endpoint MUST
  1423. // use the slow-start algorithm to increase cwnd only if the current
  1424. // congestion window is being fully utilized, an incoming SACK
  1425. // advances the Cumulative TSN Ack Point, and the data sender is not
  1426. // in Fast Recovery. Only when these three conditions are met can
  1427. // the cwnd be increased; otherwise, the cwnd MUST not be increased.
  1428. // If these conditions are met, then cwnd MUST be increased by, at
  1429. // most, the lesser of 1) the total size of the previously
  1430. // outstanding DATA chunk(s) acknowledged, and 2) the destination's
  1431. // path MTU.
  1432. if !a.inFastRecovery &&
  1433. a.pendingQueue.size() > 0 {
  1434. a.setCWND(a.CWND() + min32(uint32(totalBytesAcked), a.CWND()))
  1435. // a.cwnd += min32(uint32(totalBytesAcked), a.MTU()) // SCTP way (slow)
  1436. a.log.Tracef("[%s] updated cwnd=%d ssthresh=%d acked=%d (SS)",
  1437. a.name, a.CWND(), a.ssthresh, totalBytesAcked)
  1438. } else {
  1439. a.log.Tracef("[%s] cwnd did not grow: cwnd=%d ssthresh=%d acked=%d FR=%v pending=%d",
  1440. a.name, a.CWND(), a.ssthresh, totalBytesAcked, a.inFastRecovery, a.pendingQueue.size())
  1441. }
  1442. } else {
  1443. // RFC 4096, sec 7.2.2. Congestion Avoidance
  1444. // o Whenever cwnd is greater than ssthresh, upon each SACK arrival
  1445. // that advances the Cumulative TSN Ack Point, increase
  1446. // partial_bytes_acked by the total number of bytes of all new chunks
  1447. // acknowledged in that SACK including chunks acknowledged by the new
  1448. // Cumulative TSN Ack and by Gap Ack Blocks.
  1449. a.partialBytesAcked += uint32(totalBytesAcked)
  1450. // o When partial_bytes_acked is equal to or greater than cwnd and
  1451. // before the arrival of the SACK the sender had cwnd or more bytes
  1452. // of data outstanding (i.e., before arrival of the SACK, flight size
  1453. // was greater than or equal to cwnd), increase cwnd by MTU, and
  1454. // reset partial_bytes_acked to (partial_bytes_acked - cwnd).
  1455. if a.partialBytesAcked >= a.CWND() && a.pendingQueue.size() > 0 {
  1456. a.partialBytesAcked -= a.CWND()
  1457. a.setCWND(a.CWND() + a.MTU())
  1458. a.log.Tracef("[%s] updated cwnd=%d ssthresh=%d acked=%d (CA)",
  1459. a.name, a.CWND(), a.ssthresh, totalBytesAcked)
  1460. }
  1461. }
  1462. }
  1463. // The caller should hold the lock.
  1464. func (a *Association) processFastRetransmission(cumTSNAckPoint, htna uint32, cumTSNAckPointAdvanced bool) error {
  1465. // HTNA algorithm - RFC 4960 Sec 7.2.4
  1466. // Increment missIndicator of each chunks that the SACK reported missing
  1467. // when either of the following is met:
  1468. // a) Not in fast-recovery
  1469. // miss indications are incremented only for missing TSNs prior to the
  1470. // highest TSN newly acknowledged in the SACK.
  1471. // b) In fast-recovery AND the Cumulative TSN Ack Point advanced
  1472. // the miss indications are incremented for all TSNs reported missing
  1473. // in the SACK.
  1474. if !a.inFastRecovery || (a.inFastRecovery && cumTSNAckPointAdvanced) {
  1475. var maxTSN uint32
  1476. if !a.inFastRecovery {
  1477. // a) increment only for missing TSNs prior to the HTNA
  1478. maxTSN = htna
  1479. } else {
  1480. // b) increment for all TSNs reported missing
  1481. maxTSN = cumTSNAckPoint + uint32(a.inflightQueue.size()) + 1
  1482. }
  1483. for tsn := cumTSNAckPoint + 1; sna32LT(tsn, maxTSN); tsn++ {
  1484. c, ok := a.inflightQueue.get(tsn)
  1485. if !ok {
  1486. return fmt.Errorf("%w: %v", ErrTSNRequestNotExist, tsn)
  1487. }
  1488. if !c.acked && !c.abandoned() && c.missIndicator < 3 {
  1489. c.missIndicator++
  1490. if c.missIndicator == 3 {
  1491. if !a.inFastRecovery {
  1492. // 2) If not in Fast Recovery, adjust the ssthresh and cwnd of the
  1493. // destination address(es) to which the missing DATA chunks were
  1494. // last sent, according to the formula described in Section 7.2.3.
  1495. a.inFastRecovery = true
  1496. a.fastRecoverExitPoint = htna
  1497. a.ssthresh = max32(a.CWND()/2, 4*a.MTU())
  1498. a.setCWND(a.ssthresh)
  1499. a.partialBytesAcked = 0
  1500. a.willRetransmitFast = true
  1501. a.log.Tracef("[%s] updated cwnd=%d ssthresh=%d inflight=%d (FR)",
  1502. a.name, a.CWND(), a.ssthresh, a.inflightQueue.getNumBytes())
  1503. }
  1504. }
  1505. }
  1506. }
  1507. }
  1508. if a.inFastRecovery && cumTSNAckPointAdvanced {
  1509. a.willRetransmitFast = true
  1510. }
  1511. return nil
  1512. }
  1513. // The caller should hold the lock.
  1514. func (a *Association) handleSack(d *chunkSelectiveAck) error {
  1515. a.log.Tracef("[%s] SACK: cumTSN=%d a_rwnd=%d", a.name, d.cumulativeTSNAck, d.advertisedReceiverWindowCredit)
  1516. state := a.getState()
  1517. if state != established && state != shutdownPending && state != shutdownReceived {
  1518. return nil
  1519. }
  1520. a.stats.incSACKsReceived()
  1521. if sna32GT(a.cumulativeTSNAckPoint, d.cumulativeTSNAck) {
  1522. // RFC 4960 sec 6.2.1. Processing a Received SACK
  1523. // D)
  1524. // i) If Cumulative TSN Ack is less than the Cumulative TSN Ack
  1525. // Point, then drop the SACK. Since Cumulative TSN Ack is
  1526. // monotonically increasing, a SACK whose Cumulative TSN Ack is
  1527. // less than the Cumulative TSN Ack Point indicates an out-of-
  1528. // order SACK.
  1529. a.log.Debugf("[%s] SACK Cumulative ACK %v is older than ACK point %v",
  1530. a.name,
  1531. d.cumulativeTSNAck,
  1532. a.cumulativeTSNAckPoint)
  1533. return nil
  1534. }
  1535. // Process selective ack
  1536. bytesAckedPerStream, htna, err := a.processSelectiveAck(d)
  1537. if err != nil {
  1538. return err
  1539. }
  1540. var totalBytesAcked int
  1541. for _, nBytesAcked := range bytesAckedPerStream {
  1542. totalBytesAcked += nBytesAcked
  1543. }
  1544. cumTSNAckPointAdvanced := false
  1545. if sna32LT(a.cumulativeTSNAckPoint, d.cumulativeTSNAck) {
  1546. a.log.Tracef("[%s] SACK: cumTSN advanced: %d -> %d",
  1547. a.name,
  1548. a.cumulativeTSNAckPoint,
  1549. d.cumulativeTSNAck)
  1550. a.cumulativeTSNAckPoint = d.cumulativeTSNAck
  1551. cumTSNAckPointAdvanced = true
  1552. a.onCumulativeTSNAckPointAdvanced(totalBytesAcked)
  1553. }
  1554. for si, nBytesAcked := range bytesAckedPerStream {
  1555. if s, ok := a.streams[si]; ok {
  1556. a.lock.Unlock()
  1557. s.onBufferReleased(nBytesAcked)
  1558. a.lock.Lock()
  1559. }
  1560. }
  1561. // New rwnd value
  1562. // RFC 4960 sec 6.2.1. Processing a Received SACK
  1563. // D)
  1564. // ii) Set rwnd equal to the newly received a_rwnd minus the number
  1565. // of bytes still outstanding after processing the Cumulative
  1566. // TSN Ack and the Gap Ack Blocks.
  1567. // bytes acked were already subtracted by markAsAcked() method
  1568. bytesOutstanding := uint32(a.inflightQueue.getNumBytes())
  1569. if bytesOutstanding >= d.advertisedReceiverWindowCredit {
  1570. a.setRWND(0)
  1571. } else {
  1572. a.setRWND(d.advertisedReceiverWindowCredit - bytesOutstanding)
  1573. }
  1574. err = a.processFastRetransmission(d.cumulativeTSNAck, htna, cumTSNAckPointAdvanced)
  1575. if err != nil {
  1576. return err
  1577. }
  1578. if a.useForwardTSN {
  1579. // RFC 3758 Sec 3.5 C1
  1580. if sna32LT(a.advancedPeerTSNAckPoint, a.cumulativeTSNAckPoint) {
  1581. a.advancedPeerTSNAckPoint = a.cumulativeTSNAckPoint
  1582. }
  1583. // RFC 3758 Sec 3.5 C2
  1584. for i := a.advancedPeerTSNAckPoint + 1; ; i++ {
  1585. c, ok := a.inflightQueue.get(i)
  1586. if !ok {
  1587. break
  1588. }
  1589. if !c.abandoned() {
  1590. break
  1591. }
  1592. a.advancedPeerTSNAckPoint = i
  1593. }
  1594. // RFC 3758 Sec 3.5 C3
  1595. if sna32GT(a.advancedPeerTSNAckPoint, a.cumulativeTSNAckPoint) {
  1596. a.willSendForwardTSN = true
  1597. }
  1598. a.awakeWriteLoop()
  1599. }
  1600. a.postprocessSack(state, cumTSNAckPointAdvanced)
  1601. return nil
  1602. }
  1603. // The caller must hold the lock. This method was only added because the
  1604. // linter was complaining about the "cognitive complexity" of handleSack.
  1605. func (a *Association) postprocessSack(state uint32, shouldAwakeWriteLoop bool) {
  1606. switch {
  1607. case a.inflightQueue.size() > 0:
  1608. // Start timer. (noop if already started)
  1609. a.log.Tracef("[%s] T3-rtx timer start (pt3)", a.name)
  1610. a.t3RTX.start(a.rtoMgr.getRTO())
  1611. case state == shutdownPending:
  1612. // No more outstanding, send shutdown.
  1613. shouldAwakeWriteLoop = true
  1614. a.willSendShutdown = true
  1615. a.setState(shutdownSent)
  1616. case state == shutdownReceived:
  1617. // No more outstanding, send shutdown ack.
  1618. shouldAwakeWriteLoop = true
  1619. a.willSendShutdownAck = true
  1620. a.setState(shutdownAckSent)
  1621. }
  1622. if shouldAwakeWriteLoop {
  1623. a.awakeWriteLoop()
  1624. }
  1625. }
  1626. // The caller should hold the lock.
  1627. func (a *Association) handleShutdown(_ *chunkShutdown) {
  1628. state := a.getState()
  1629. switch state {
  1630. case established:
  1631. if a.inflightQueue.size() > 0 {
  1632. a.setState(shutdownReceived)
  1633. } else {
  1634. // No more outstanding, send shutdown ack.
  1635. a.willSendShutdownAck = true
  1636. a.setState(shutdownAckSent)
  1637. a.awakeWriteLoop()
  1638. }
  1639. // a.cumulativeTSNAckPoint = c.cumulativeTSNAck
  1640. case shutdownSent:
  1641. a.willSendShutdownAck = true
  1642. a.setState(shutdownAckSent)
  1643. a.awakeWriteLoop()
  1644. }
  1645. }
  1646. // The caller should hold the lock.
  1647. func (a *Association) handleShutdownAck(_ *chunkShutdownAck) {
  1648. state := a.getState()
  1649. if state == shutdownSent || state == shutdownAckSent {
  1650. a.t2Shutdown.stop()
  1651. a.willSendShutdownComplete = true
  1652. a.awakeWriteLoop()
  1653. }
  1654. }
  1655. func (a *Association) handleShutdownComplete(_ *chunkShutdownComplete) error {
  1656. state := a.getState()
  1657. if state == shutdownAckSent {
  1658. a.t2Shutdown.stop()
  1659. return a.close()
  1660. }
  1661. return nil
  1662. }
  1663. func (a *Association) handleAbort(c *chunkAbort) error {
  1664. var errStr string
  1665. for _, e := range c.errorCauses {
  1666. errStr += fmt.Sprintf("(%s)", e)
  1667. }
  1668. _ = a.close()
  1669. return fmt.Errorf("[%s] %w: %s", a.name, ErrChunk, errStr)
  1670. }
  1671. // createForwardTSN generates ForwardTSN chunk.
  1672. // This method will be be called if useForwardTSN is set to false.
  1673. // The caller should hold the lock.
  1674. func (a *Association) createForwardTSN() *chunkForwardTSN {
  1675. // RFC 3758 Sec 3.5 C4
  1676. streamMap := map[uint16]uint16{} // to report only once per SI
  1677. for i := a.cumulativeTSNAckPoint + 1; sna32LTE(i, a.advancedPeerTSNAckPoint); i++ {
  1678. c, ok := a.inflightQueue.get(i)
  1679. if !ok {
  1680. break
  1681. }
  1682. ssn, ok := streamMap[c.streamIdentifier]
  1683. if !ok {
  1684. streamMap[c.streamIdentifier] = c.streamSequenceNumber
  1685. } else if sna16LT(ssn, c.streamSequenceNumber) {
  1686. // to report only once with greatest SSN
  1687. streamMap[c.streamIdentifier] = c.streamSequenceNumber
  1688. }
  1689. }
  1690. fwdtsn := &chunkForwardTSN{
  1691. newCumulativeTSN: a.advancedPeerTSNAckPoint,
  1692. streams: []chunkForwardTSNStream{},
  1693. }
  1694. var streamStr string
  1695. for si, ssn := range streamMap {
  1696. streamStr += fmt.Sprintf("(si=%d ssn=%d)", si, ssn)
  1697. fwdtsn.streams = append(fwdtsn.streams, chunkForwardTSNStream{
  1698. identifier: si,
  1699. sequence: ssn,
  1700. })
  1701. }
  1702. a.log.Tracef("[%s] building fwdtsn: newCumulativeTSN=%d cumTSN=%d - %s", a.name, fwdtsn.newCumulativeTSN, a.cumulativeTSNAckPoint, streamStr)
  1703. return fwdtsn
  1704. }
  1705. // createPacket wraps chunks in a packet.
  1706. // The caller should hold the read lock.
  1707. func (a *Association) createPacket(cs []chunk) *packet {
  1708. return &packet{
  1709. verificationTag: a.peerVerificationTag,
  1710. sourcePort: a.sourcePort,
  1711. destinationPort: a.destinationPort,
  1712. chunks: cs,
  1713. }
  1714. }
  1715. // The caller should hold the lock.
  1716. func (a *Association) handleReconfig(c *chunkReconfig) ([]*packet, error) {
  1717. a.log.Tracef("[%s] handleReconfig", a.name)
  1718. pp := make([]*packet, 0)
  1719. p, err := a.handleReconfigParam(c.paramA)
  1720. if err != nil {
  1721. return nil, err
  1722. }
  1723. if p != nil {
  1724. pp = append(pp, p)
  1725. }
  1726. if c.paramB != nil {
  1727. p, err = a.handleReconfigParam(c.paramB)
  1728. if err != nil {
  1729. return nil, err
  1730. }
  1731. if p != nil {
  1732. pp = append(pp, p)
  1733. }
  1734. }
  1735. return pp, nil
  1736. }
  1737. // The caller should hold the lock.
  1738. func (a *Association) handleForwardTSN(c *chunkForwardTSN) []*packet {
  1739. a.log.Tracef("[%s] FwdTSN: %s", a.name, c.String())
  1740. if !a.useForwardTSN {
  1741. a.log.Warn("[%s] received FwdTSN but not enabled")
  1742. // Return an error chunk
  1743. cerr := &chunkError{
  1744. errorCauses: []errorCause{&errorCauseUnrecognizedChunkType{}},
  1745. }
  1746. outbound := &packet{}
  1747. outbound.verificationTag = a.peerVerificationTag
  1748. outbound.sourcePort = a.sourcePort
  1749. outbound.destinationPort = a.destinationPort
  1750. outbound.chunks = []chunk{cerr}
  1751. return []*packet{outbound}
  1752. }
  1753. // From RFC 3758 Sec 3.6:
  1754. // Note, if the "New Cumulative TSN" value carried in the arrived
  1755. // FORWARD TSN chunk is found to be behind or at the current cumulative
  1756. // TSN point, the data receiver MUST treat this FORWARD TSN as out-of-
  1757. // date and MUST NOT update its Cumulative TSN. The receiver SHOULD
  1758. // send a SACK to its peer (the sender of the FORWARD TSN) since such a
  1759. // duplicate may indicate the previous SACK was lost in the network.
  1760. a.log.Tracef("[%s] should send ack? newCumTSN=%d peerLastTSN=%d",
  1761. a.name, c.newCumulativeTSN, a.peerLastTSN)
  1762. if sna32LTE(c.newCumulativeTSN, a.peerLastTSN) {
  1763. a.log.Tracef("[%s] sending ack on Forward TSN", a.name)
  1764. a.ackState = ackStateImmediate
  1765. a.ackTimer.stop()
  1766. a.awakeWriteLoop()
  1767. return nil
  1768. }
  1769. // From RFC 3758 Sec 3.6:
  1770. // the receiver MUST perform the same TSN handling, including duplicate
  1771. // detection, gap detection, SACK generation, cumulative TSN
  1772. // advancement, etc. as defined in RFC 2960 [2]---with the following
  1773. // exceptions and additions.
  1774. // When a FORWARD TSN chunk arrives, the data receiver MUST first update
  1775. // its cumulative TSN point to the value carried in the FORWARD TSN
  1776. // chunk,
  1777. // Advance peerLastTSN
  1778. for sna32LT(a.peerLastTSN, c.newCumulativeTSN) {
  1779. a.payloadQueue.pop(a.peerLastTSN + 1) // may not exist
  1780. a.peerLastTSN++
  1781. }
  1782. // Report new peerLastTSN value and abandoned largest SSN value to
  1783. // corresponding streams so that the abandoned chunks can be removed
  1784. // from the reassemblyQueue.
  1785. for _, forwarded := range c.streams {
  1786. if s, ok := a.streams[forwarded.identifier]; ok {
  1787. s.handleForwardTSNForOrdered(forwarded.sequence)
  1788. }
  1789. }
  1790. // TSN may be forewared for unordered chunks. ForwardTSN chunk does not
  1791. // report which stream identifier it skipped for unordered chunks.
  1792. // Therefore, we need to broadcast this event to all existing streams for
  1793. // unordered chunks.
  1794. // See https://github.com/pion/sctp/issues/106
  1795. for _, s := range a.streams {
  1796. s.handleForwardTSNForUnordered(c.newCumulativeTSN)
  1797. }
  1798. return a.handlePeerLastTSNAndAcknowledgement(false)
  1799. }
  1800. func (a *Association) sendResetRequest(streamIdentifier uint16) error {
  1801. a.lock.Lock()
  1802. defer a.lock.Unlock()
  1803. state := a.getState()
  1804. if state != established {
  1805. return fmt.Errorf("%w: state=%s", ErrResetPacketInStateNotExist,
  1806. getAssociationStateString(state))
  1807. }
  1808. // Create DATA chunk which only contains valid stream identifier with
  1809. // nil userData and use it as a EOS from the stream.
  1810. c := &chunkPayloadData{
  1811. streamIdentifier: streamIdentifier,
  1812. beginningFragment: true,
  1813. endingFragment: true,
  1814. userData: nil,
  1815. }
  1816. a.pendingQueue.push(c)
  1817. a.awakeWriteLoop()
  1818. return nil
  1819. }
  1820. // The caller should hold the lock.
  1821. func (a *Association) handleReconfigParam(raw param) (*packet, error) {
  1822. switch p := raw.(type) {
  1823. case *paramOutgoingResetRequest:
  1824. a.log.Tracef("[%s] handleReconfigParam (OutgoingResetRequest)", a.name)
  1825. if a.peerLastTSN < p.senderLastTSN && len(a.reconfigRequests) >= maxReconfigRequests {
  1826. // We have too many reconfig requests outstanding. Drop the request and let
  1827. // the peer retransmit. A well behaved peer should only have 1 outstanding
  1828. // reconfig request.
  1829. //
  1830. // RFC 6525: https://www.rfc-editor.org/rfc/rfc6525.html#section-5.1.1
  1831. // At any given time, there MUST NOT be more than one request in flight.
  1832. // So, if the Re-configuration Timer is running and the RE-CONFIG chunk
  1833. // contains at least one request parameter, the chunk MUST be buffered.
  1834. // chrome: https://chromium.googlesource.com/external/webrtc/+/refs/heads/main/net/dcsctp/socket/stream_reset_handler.cc#271
  1835. return nil, fmt.Errorf("%w: %d", ErrTooManyReconfigRequests, len(a.reconfigRequests))
  1836. }
  1837. a.reconfigRequests[p.reconfigRequestSequenceNumber] = p
  1838. resp := a.resetStreamsIfAny(p)
  1839. if resp != nil {
  1840. return resp, nil
  1841. }
  1842. return nil, nil //nolint:nilnil
  1843. case *paramReconfigResponse:
  1844. a.log.Tracef("[%s] handleReconfigParam (ReconfigResponse)", a.name)
  1845. if p.result == reconfigResultInProgress {
  1846. // RFC 6525: https://www.rfc-editor.org/rfc/rfc6525.html#section-5.2.7
  1847. //
  1848. // If the Result field indicates "In progress", the timer for the
  1849. // Re-configuration Request Sequence Number is started again. If
  1850. // the timer runs out, the RE-CONFIG chunk MUST be retransmitted
  1851. // but the corresponding error counters MUST NOT be incremented.
  1852. if _, ok := a.reconfigs[p.reconfigResponseSequenceNumber]; ok {
  1853. a.tReconfig.stop()
  1854. a.tReconfig.start(a.rtoMgr.getRTO())
  1855. }
  1856. return nil, nil //nolint:nilnil
  1857. }
  1858. delete(a.reconfigs, p.reconfigResponseSequenceNumber)
  1859. if len(a.reconfigs) == 0 {
  1860. a.tReconfig.stop()
  1861. }
  1862. return nil, nil //nolint:nilnil
  1863. default:
  1864. return nil, fmt.Errorf("%w: %t", ErrParamterType, p)
  1865. }
  1866. }
  1867. // The caller should hold the lock.
  1868. func (a *Association) resetStreamsIfAny(p *paramOutgoingResetRequest) *packet {
  1869. result := reconfigResultSuccessPerformed
  1870. if sna32LTE(p.senderLastTSN, a.peerLastTSN) {
  1871. a.log.Debugf("[%s] resetStream(): senderLastTSN=%d <= peerLastTSN=%d",
  1872. a.name, p.senderLastTSN, a.peerLastTSN)
  1873. for _, id := range p.streamIdentifiers {
  1874. s, ok := a.streams[id]
  1875. if !ok {
  1876. continue
  1877. }
  1878. a.lock.Unlock()
  1879. s.onInboundStreamReset()
  1880. a.lock.Lock()
  1881. a.log.Debugf("[%s] deleting stream %d", a.name, id)
  1882. delete(a.streams, s.streamIdentifier)
  1883. }
  1884. delete(a.reconfigRequests, p.reconfigRequestSequenceNumber)
  1885. } else {
  1886. a.log.Debugf("[%s] resetStream(): senderLastTSN=%d > peerLastTSN=%d",
  1887. a.name, p.senderLastTSN, a.peerLastTSN)
  1888. result = reconfigResultInProgress
  1889. }
  1890. return a.createPacket([]chunk{&chunkReconfig{
  1891. paramA: &paramReconfigResponse{
  1892. reconfigResponseSequenceNumber: p.reconfigRequestSequenceNumber,
  1893. result: result,
  1894. },
  1895. }})
  1896. }
  1897. // Move the chunk peeked with a.pendingQueue.peek() to the inflightQueue.
  1898. // The caller should hold the lock.
  1899. func (a *Association) movePendingDataChunkToInflightQueue(c *chunkPayloadData) {
  1900. if err := a.pendingQueue.pop(c); err != nil {
  1901. a.log.Errorf("[%s] failed to pop from pending queue: %s", a.name, err.Error())
  1902. }
  1903. // Mark all fragements are in-flight now
  1904. if c.endingFragment {
  1905. c.setAllInflight()
  1906. }
  1907. // Assign TSN
  1908. c.tsn = a.generateNextTSN()
  1909. c.since = time.Now() // use to calculate RTT and also for maxPacketLifeTime
  1910. c.nSent = 1 // being sent for the first time
  1911. a.checkPartialReliabilityStatus(c)
  1912. a.log.Tracef("[%s] sending ppi=%d tsn=%d ssn=%d sent=%d len=%d (%v,%v)",
  1913. a.name, c.payloadType, c.tsn, c.streamSequenceNumber, c.nSent, len(c.userData), c.beginningFragment, c.endingFragment)
  1914. a.inflightQueue.pushNoCheck(c)
  1915. }
  1916. // popPendingDataChunksToSend pops chunks from the pending queues as many as
  1917. // the cwnd and rwnd allows to send.
  1918. // The caller should hold the lock.
  1919. func (a *Association) popPendingDataChunksToSend() ([]*chunkPayloadData, []uint16) {
  1920. chunks := []*chunkPayloadData{}
  1921. var sisToReset []uint16 // stream identifieres to reset
  1922. if a.pendingQueue.size() > 0 {
  1923. // RFC 4960 sec 6.1. Transmission of DATA Chunks
  1924. // A) At any given time, the data sender MUST NOT transmit new data to
  1925. // any destination transport address if its peer's rwnd indicates
  1926. // that the peer has no buffer space (i.e., rwnd is 0; see Section
  1927. // 6.2.1). However, regardless of the value of rwnd (including if it
  1928. // is 0), the data sender can always have one DATA chunk in flight to
  1929. // the receiver if allowed by cwnd (see rule B, below).
  1930. for {
  1931. c := a.pendingQueue.peek()
  1932. if c == nil {
  1933. break // no more pending data
  1934. }
  1935. dataLen := uint32(len(c.userData))
  1936. if dataLen == 0 {
  1937. sisToReset = append(sisToReset, c.streamIdentifier)
  1938. err := a.pendingQueue.pop(c)
  1939. if err != nil {
  1940. a.log.Errorf("failed to pop from pending queue: %s", err.Error())
  1941. }
  1942. continue
  1943. }
  1944. if uint32(a.inflightQueue.getNumBytes())+dataLen > a.CWND() {
  1945. break // would exceeds cwnd
  1946. }
  1947. if dataLen > a.rwnd {
  1948. break // no more rwnd
  1949. }
  1950. a.setRWND(a.RWND() - dataLen)
  1951. a.movePendingDataChunkToInflightQueue(c)
  1952. chunks = append(chunks, c)
  1953. }
  1954. // the data sender can always have one DATA chunk in flight to the receiver
  1955. if len(chunks) == 0 && a.inflightQueue.size() == 0 {
  1956. // Send zero window probe
  1957. c := a.pendingQueue.peek()
  1958. if c != nil {
  1959. a.movePendingDataChunkToInflightQueue(c)
  1960. chunks = append(chunks, c)
  1961. }
  1962. }
  1963. }
  1964. return chunks, sisToReset
  1965. }
  1966. // bundleDataChunksIntoPackets packs DATA chunks into packets. It tries to bundle
  1967. // DATA chunks into a packet so long as the resulting packet size does not exceed
  1968. // the path MTU.
  1969. // The caller should hold the lock.
  1970. func (a *Association) bundleDataChunksIntoPackets(chunks []*chunkPayloadData) []*packet {
  1971. packets := []*packet{}
  1972. chunksToSend := []chunk{}
  1973. bytesInPacket := int(commonHeaderSize)
  1974. for _, c := range chunks {
  1975. // RFC 4960 sec 6.1. Transmission of DATA Chunks
  1976. // Multiple DATA chunks committed for transmission MAY be bundled in a
  1977. // single packet. Furthermore, DATA chunks being retransmitted MAY be
  1978. // bundled with new DATA chunks, as long as the resulting packet size
  1979. // does not exceed the path MTU.
  1980. chunkSizeInPacket := int(dataChunkHeaderSize) + len(c.userData)
  1981. chunkSizeInPacket += getPadding(chunkSizeInPacket)
  1982. if bytesInPacket+chunkSizeInPacket > int(a.MTU()) {
  1983. packets = append(packets, a.createPacket(chunksToSend))
  1984. chunksToSend = []chunk{}
  1985. bytesInPacket = int(commonHeaderSize)
  1986. }
  1987. chunksToSend = append(chunksToSend, c)
  1988. bytesInPacket += chunkSizeInPacket
  1989. }
  1990. if len(chunksToSend) > 0 {
  1991. packets = append(packets, a.createPacket(chunksToSend))
  1992. }
  1993. return packets
  1994. }
  1995. // sendPayloadData sends the data chunks.
  1996. func (a *Association) sendPayloadData(chunks []*chunkPayloadData) error {
  1997. a.lock.Lock()
  1998. defer a.lock.Unlock()
  1999. state := a.getState()
  2000. if state != established {
  2001. return fmt.Errorf("%w: state=%s", ErrPayloadDataStateNotExist,
  2002. getAssociationStateString(state))
  2003. }
  2004. // Push the chunks into the pending queue first.
  2005. for _, c := range chunks {
  2006. a.pendingQueue.push(c)
  2007. }
  2008. a.awakeWriteLoop()
  2009. return nil
  2010. }
  2011. // The caller should hold the lock.
  2012. func (a *Association) checkPartialReliabilityStatus(c *chunkPayloadData) {
  2013. if !a.useForwardTSN {
  2014. return
  2015. }
  2016. // draft-ietf-rtcweb-data-protocol-09.txt section 6
  2017. // 6. Procedures
  2018. // All Data Channel Establishment Protocol messages MUST be sent using
  2019. // ordered delivery and reliable transmission.
  2020. //
  2021. if c.payloadType == PayloadTypeWebRTCDCEP {
  2022. return
  2023. }
  2024. // PR-SCTP
  2025. if s, ok := a.streams[c.streamIdentifier]; ok {
  2026. s.lock.RLock()
  2027. if s.reliabilityType == ReliabilityTypeRexmit {
  2028. if c.nSent >= s.reliabilityValue {
  2029. c.setAbandoned(true)
  2030. a.log.Tracef("[%s] marked as abandoned: tsn=%d ppi=%d (remix: %d)", a.name, c.tsn, c.payloadType, c.nSent)
  2031. }
  2032. } else if s.reliabilityType == ReliabilityTypeTimed {
  2033. elapsed := int64(time.Since(c.since).Seconds() * 1000)
  2034. if elapsed >= int64(s.reliabilityValue) {
  2035. c.setAbandoned(true)
  2036. a.log.Tracef("[%s] marked as abandoned: tsn=%d ppi=%d (timed: %d)", a.name, c.tsn, c.payloadType, elapsed)
  2037. }
  2038. }
  2039. s.lock.RUnlock()
  2040. } else {
  2041. a.log.Errorf("[%s] stream %d not found)", a.name, c.streamIdentifier)
  2042. }
  2043. }
  2044. // getDataPacketsToRetransmit is called when T3-rtx is timed out and retransmit outstanding data chunks
  2045. // that are not acked or abandoned yet.
  2046. // The caller should hold the lock.
  2047. func (a *Association) getDataPacketsToRetransmit() []*packet {
  2048. awnd := min32(a.CWND(), a.RWND())
  2049. chunks := []*chunkPayloadData{}
  2050. var bytesToSend int
  2051. var done bool
  2052. for i := 0; !done; i++ {
  2053. c, ok := a.inflightQueue.get(a.cumulativeTSNAckPoint + uint32(i) + 1)
  2054. if !ok {
  2055. break // end of pending data
  2056. }
  2057. if !c.retransmit {
  2058. continue
  2059. }
  2060. if i == 0 && int(a.RWND()) < len(c.userData) {
  2061. // Send it as a zero window probe
  2062. done = true
  2063. } else if bytesToSend+len(c.userData) > int(awnd) {
  2064. break
  2065. }
  2066. // reset the retransmit flag not to retransmit again before the next
  2067. // t3-rtx timer fires
  2068. c.retransmit = false
  2069. bytesToSend += len(c.userData)
  2070. c.nSent++
  2071. a.checkPartialReliabilityStatus(c)
  2072. a.log.Tracef("[%s] retransmitting tsn=%d ssn=%d sent=%d", a.name, c.tsn, c.streamSequenceNumber, c.nSent)
  2073. chunks = append(chunks, c)
  2074. }
  2075. return a.bundleDataChunksIntoPackets(chunks)
  2076. }
  2077. // generateNextTSN returns the myNextTSN and increases it. The caller should hold the lock.
  2078. // The caller should hold the lock.
  2079. func (a *Association) generateNextTSN() uint32 {
  2080. tsn := a.myNextTSN
  2081. a.myNextTSN++
  2082. return tsn
  2083. }
  2084. // generateNextRSN returns the myNextRSN and increases it. The caller should hold the lock.
  2085. // The caller should hold the lock.
  2086. func (a *Association) generateNextRSN() uint32 {
  2087. rsn := a.myNextRSN
  2088. a.myNextRSN++
  2089. return rsn
  2090. }
  2091. func (a *Association) createSelectiveAckChunk() *chunkSelectiveAck {
  2092. sack := &chunkSelectiveAck{}
  2093. sack.cumulativeTSNAck = a.peerLastTSN
  2094. sack.advertisedReceiverWindowCredit = a.getMyReceiverWindowCredit()
  2095. sack.duplicateTSN = a.payloadQueue.popDuplicates()
  2096. sack.gapAckBlocks = a.payloadQueue.getGapAckBlocks(a.peerLastTSN)
  2097. return sack
  2098. }
  2099. func pack(p *packet) []*packet {
  2100. return []*packet{p}
  2101. }
  2102. func (a *Association) handleChunksStart() {
  2103. a.lock.Lock()
  2104. defer a.lock.Unlock()
  2105. a.stats.incPacketsReceived()
  2106. a.delayedAckTriggered = false
  2107. a.immediateAckTriggered = false
  2108. }
  2109. func (a *Association) handleChunksEnd() {
  2110. a.lock.Lock()
  2111. defer a.lock.Unlock()
  2112. if a.immediateAckTriggered {
  2113. a.ackState = ackStateImmediate
  2114. a.ackTimer.stop()
  2115. a.awakeWriteLoop()
  2116. } else if a.delayedAckTriggered {
  2117. // Will send delayed ack in the next ack timeout
  2118. a.ackState = ackStateDelay
  2119. a.ackTimer.start()
  2120. }
  2121. }
  2122. func (a *Association) handleChunk(p *packet, c chunk) error {
  2123. a.lock.Lock()
  2124. defer a.lock.Unlock()
  2125. var packets []*packet
  2126. var err error
  2127. if _, err = c.check(); err != nil {
  2128. a.log.Errorf("[%s] failed validating chunk: %s ", a.name, err)
  2129. return nil
  2130. }
  2131. isAbort := false
  2132. switch c := c.(type) {
  2133. // Note: We do not do the following for chunkInit, chunkInitAck, and chunkCookieEcho:
  2134. // If an endpoint receives an INIT, INIT ACK, or COOKIE ECHO chunk but decides not to establish the
  2135. // new association due to missing mandatory parameters in the received INIT or INIT ACK chunk, invalid
  2136. // parameter values, or lack of local resources, it SHOULD respond with an ABORT chunk.
  2137. case *chunkInit:
  2138. packets, err = a.handleInit(p, c)
  2139. case *chunkInitAck:
  2140. err = a.handleInitAck(p, c)
  2141. case *chunkAbort:
  2142. isAbort = true
  2143. err = a.handleAbort(c)
  2144. case *chunkError:
  2145. var errStr string
  2146. for _, e := range c.errorCauses {
  2147. errStr += fmt.Sprintf("(%s)", e)
  2148. }
  2149. a.log.Debugf("[%s] Error chunk, with following errors: %s", a.name, errStr)
  2150. // Note: chunkHeartbeatAck not handled?
  2151. case *chunkHeartbeat:
  2152. packets = a.handleHeartbeat(c)
  2153. case *chunkCookieEcho:
  2154. packets = a.handleCookieEcho(c)
  2155. case *chunkCookieAck:
  2156. a.handleCookieAck()
  2157. case *chunkPayloadData:
  2158. packets = a.handleData(c)
  2159. case *chunkSelectiveAck:
  2160. err = a.handleSack(c)
  2161. case *chunkReconfig:
  2162. packets, err = a.handleReconfig(c)
  2163. case *chunkForwardTSN:
  2164. packets = a.handleForwardTSN(c)
  2165. case *chunkShutdown:
  2166. a.handleShutdown(c)
  2167. case *chunkShutdownAck:
  2168. a.handleShutdownAck(c)
  2169. case *chunkShutdownComplete:
  2170. err = a.handleShutdownComplete(c)
  2171. default:
  2172. err = ErrChunkTypeUnhandled
  2173. }
  2174. // Log and return, the only condition that is fatal is a ABORT chunk
  2175. if err != nil {
  2176. if isAbort {
  2177. return err
  2178. }
  2179. a.log.Errorf("Failed to handle chunk: %v", err)
  2180. return nil
  2181. }
  2182. if len(packets) > 0 {
  2183. a.controlQueue.pushAll(packets)
  2184. a.awakeWriteLoop()
  2185. }
  2186. return nil
  2187. }
  2188. func (a *Association) onRetransmissionTimeout(id int, nRtos uint) {
  2189. a.lock.Lock()
  2190. defer a.lock.Unlock()
  2191. // TSN hasn't been incremented in 3 attempts. Speculatively
  2192. // toggle ZeroChecksum because old Pion versions had a broken implementation
  2193. if a.cumulativeTSNAckPoint+1 == a.initialTSN && nRtos%3 == 0 {
  2194. a.sendZeroChecksum = !a.sendZeroChecksum
  2195. }
  2196. if id == timerT1Init {
  2197. err := a.sendInit()
  2198. if err != nil {
  2199. a.log.Debugf("[%s] failed to retransmit init (nRtos=%d): %v", a.name, nRtos, err)
  2200. }
  2201. return
  2202. }
  2203. if id == timerT1Cookie {
  2204. err := a.sendCookieEcho()
  2205. if err != nil {
  2206. a.log.Debugf("[%s] failed to retransmit cookie-echo (nRtos=%d): %v", a.name, nRtos, err)
  2207. }
  2208. return
  2209. }
  2210. if id == timerT2Shutdown {
  2211. a.log.Debugf("[%s] retransmission of shutdown timeout (nRtos=%d): %v", a.name, nRtos)
  2212. state := a.getState()
  2213. switch state {
  2214. case shutdownSent:
  2215. a.willSendShutdown = true
  2216. a.awakeWriteLoop()
  2217. case shutdownAckSent:
  2218. a.willSendShutdownAck = true
  2219. a.awakeWriteLoop()
  2220. }
  2221. }
  2222. if id == timerT3RTX {
  2223. a.stats.incT3Timeouts()
  2224. // RFC 4960 sec 6.3.3
  2225. // E1) For the destination address for which the timer expires, adjust
  2226. // its ssthresh with rules defined in Section 7.2.3 and set the
  2227. // cwnd <- MTU.
  2228. // RFC 4960 sec 7.2.3
  2229. // When the T3-rtx timer expires on an address, SCTP should perform slow
  2230. // start by:
  2231. // ssthresh = max(cwnd/2, 4*MTU)
  2232. // cwnd = 1*MTU
  2233. a.ssthresh = max32(a.CWND()/2, 4*a.MTU())
  2234. a.setCWND(a.MTU())
  2235. a.log.Tracef("[%s] updated cwnd=%d ssthresh=%d inflight=%d (RTO)",
  2236. a.name, a.CWND(), a.ssthresh, a.inflightQueue.getNumBytes())
  2237. // RFC 3758 sec 3.5
  2238. // A5) Any time the T3-rtx timer expires, on any destination, the sender
  2239. // SHOULD try to advance the "Advanced.Peer.Ack.Point" by following
  2240. // the procedures outlined in C2 - C5.
  2241. if a.useForwardTSN {
  2242. // RFC 3758 Sec 3.5 C2
  2243. for i := a.advancedPeerTSNAckPoint + 1; ; i++ {
  2244. c, ok := a.inflightQueue.get(i)
  2245. if !ok {
  2246. break
  2247. }
  2248. if !c.abandoned() {
  2249. break
  2250. }
  2251. a.advancedPeerTSNAckPoint = i
  2252. }
  2253. // RFC 3758 Sec 3.5 C3
  2254. if sna32GT(a.advancedPeerTSNAckPoint, a.cumulativeTSNAckPoint) {
  2255. a.willSendForwardTSN = true
  2256. }
  2257. }
  2258. a.log.Debugf("[%s] T3-rtx timed out: nRtos=%d cwnd=%d ssthresh=%d", a.name, nRtos, a.CWND(), a.ssthresh)
  2259. /*
  2260. a.log.Debugf(" - advancedPeerTSNAckPoint=%d", a.advancedPeerTSNAckPoint)
  2261. a.log.Debugf(" - cumulativeTSNAckPoint=%d", a.cumulativeTSNAckPoint)
  2262. a.inflightQueue.updateSortedKeys()
  2263. for i, tsn := range a.inflightQueue.sorted {
  2264. if c, ok := a.inflightQueue.get(tsn); ok {
  2265. a.log.Debugf(" - [%d] tsn=%d acked=%v abandoned=%v (%v,%v) len=%d",
  2266. i, c.tsn, c.acked, c.abandoned(), c.beginningFragment, c.endingFragment, len(c.userData))
  2267. }
  2268. }
  2269. */
  2270. a.inflightQueue.markAllToRetrasmit()
  2271. a.awakeWriteLoop()
  2272. return
  2273. }
  2274. if id == timerReconfig {
  2275. a.willRetransmitReconfig = true
  2276. a.awakeWriteLoop()
  2277. }
  2278. }
  2279. func (a *Association) onRetransmissionFailure(id int) {
  2280. a.lock.Lock()
  2281. defer a.lock.Unlock()
  2282. if id == timerT1Init {
  2283. a.log.Errorf("[%s] retransmission failure: T1-init", a.name)
  2284. a.handshakeCompletedCh <- ErrHandshakeInitAck
  2285. return
  2286. }
  2287. if id == timerT1Cookie {
  2288. a.log.Errorf("[%s] retransmission failure: T1-cookie", a.name)
  2289. a.handshakeCompletedCh <- ErrHandshakeCookieEcho
  2290. return
  2291. }
  2292. if id == timerT2Shutdown {
  2293. a.log.Errorf("[%s] retransmission failure: T2-shutdown", a.name)
  2294. return
  2295. }
  2296. if id == timerT3RTX {
  2297. // T3-rtx timer will not fail by design
  2298. // Justifications:
  2299. // * ICE would fail if the connectivity is lost
  2300. // * WebRTC spec is not clear how this incident should be reported to ULP
  2301. a.log.Errorf("[%s] retransmission failure: T3-rtx (DATA)", a.name)
  2302. return
  2303. }
  2304. }
  2305. func (a *Association) onAckTimeout() {
  2306. a.lock.Lock()
  2307. defer a.lock.Unlock()
  2308. a.log.Tracef("[%s] ack timed out (ackState: %d)", a.name, a.ackState)
  2309. a.stats.incAckTimeouts()
  2310. a.ackState = ackStateImmediate
  2311. a.awakeWriteLoop()
  2312. }
  2313. // bufferedAmount returns total amount (in bytes) of currently buffered user data.
  2314. // This is used only by testing.
  2315. func (a *Association) bufferedAmount() int {
  2316. a.lock.RLock()
  2317. defer a.lock.RUnlock()
  2318. return a.pendingQueue.getNumBytes() + a.inflightQueue.getNumBytes()
  2319. }
  2320. // MaxMessageSize returns the maximum message size you can send.
  2321. func (a *Association) MaxMessageSize() uint32 {
  2322. return atomic.LoadUint32(&a.maxMessageSize)
  2323. }
  2324. // SetMaxMessageSize sets the maximum message size you can send.
  2325. func (a *Association) SetMaxMessageSize(maxMsgSize uint32) {
  2326. atomic.StoreUint32(&a.maxMessageSize, maxMsgSize)
  2327. }