association.go 71 KB

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