association.go 73 KB

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