chunk_init.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package sctp // nolint:dupl
  4. import (
  5. "errors"
  6. "fmt"
  7. )
  8. /*
  9. Init represents an SCTP Chunk of type INIT
  10. See chunkInitCommon for the fixed headers
  11. Variable Parameters Status Type Value
  12. -------------------------------------------------------------
  13. IPv4 IP (Note 1) Optional 5
  14. IPv6 IP (Note 1) Optional 6
  15. Cookie Preservative Optional 9
  16. Reserved for ECN Capable (Note 2) Optional 32768 (0x8000)
  17. Host Name IP (Note 3) Optional 11
  18. Supported IP Types (Note 4) Optional 12
  19. */
  20. type chunkInit struct {
  21. chunkHeader
  22. chunkInitCommon
  23. }
  24. // Init chunk errors
  25. var (
  26. ErrChunkTypeNotTypeInit = errors.New("ChunkType is not of type INIT")
  27. ErrChunkValueNotLongEnough = errors.New("chunk Value isn't long enough for mandatory parameters exp")
  28. ErrChunkTypeInitFlagZero = errors.New("ChunkType of type INIT flags must be all 0")
  29. ErrChunkTypeInitUnmarshalFailed = errors.New("failed to unmarshal INIT body")
  30. ErrChunkTypeInitMarshalFailed = errors.New("failed marshaling INIT common data")
  31. ErrChunkTypeInitInitateTagZero = errors.New("ChunkType of type INIT ACK InitiateTag must not be 0")
  32. ErrInitInboundStreamRequestZero = errors.New("INIT ACK inbound stream request must be > 0")
  33. ErrInitOutboundStreamRequestZero = errors.New("INIT ACK outbound stream request must be > 0")
  34. ErrInitAdvertisedReceiver1500 = errors.New("INIT ACK Advertised Receiver Window Credit (a_rwnd) must be >= 1500")
  35. )
  36. func (i *chunkInit) unmarshal(raw []byte) error {
  37. if err := i.chunkHeader.unmarshal(raw); err != nil {
  38. return err
  39. }
  40. if i.typ != ctInit {
  41. return fmt.Errorf("%w: actually is %s", ErrChunkTypeNotTypeInit, i.typ.String())
  42. } else if len(i.raw) < initChunkMinLength {
  43. return fmt.Errorf("%w: %d actual: %d", ErrChunkValueNotLongEnough, initChunkMinLength, len(i.raw))
  44. }
  45. // The Chunk Flags field in INIT is reserved, and all bits in it should
  46. // be set to 0 by the sender and ignored by the receiver. The sequence
  47. // of parameters within an INIT can be processed in any order.
  48. if i.flags != 0 {
  49. return ErrChunkTypeInitFlagZero
  50. }
  51. if err := i.chunkInitCommon.unmarshal(i.raw); err != nil {
  52. return fmt.Errorf("%w: %v", ErrChunkTypeInitUnmarshalFailed, err) //nolint:errorlint
  53. }
  54. return nil
  55. }
  56. func (i *chunkInit) marshal() ([]byte, error) {
  57. initShared, err := i.chunkInitCommon.marshal()
  58. if err != nil {
  59. return nil, fmt.Errorf("%w: %v", ErrChunkTypeInitMarshalFailed, err) //nolint:errorlint
  60. }
  61. i.chunkHeader.typ = ctInit
  62. i.chunkHeader.raw = initShared
  63. return i.chunkHeader.marshal()
  64. }
  65. func (i *chunkInit) check() (abort bool, err error) {
  66. // The receiver of the INIT (the responding end) records the value of
  67. // the Initiate Tag parameter. This value MUST be placed into the
  68. // Verification Tag field of every SCTP packet that the receiver of
  69. // the INIT transmits within this association.
  70. //
  71. // The Initiate Tag is allowed to have any value except 0. See
  72. // Section 5.3.1 for more on the selection of the tag value.
  73. //
  74. // If the value of the Initiate Tag in a received INIT chunk is found
  75. // to be 0, the receiver MUST treat it as an error and close the
  76. // association by transmitting an ABORT.
  77. if i.initiateTag == 0 {
  78. abort = true
  79. return abort, ErrChunkTypeInitInitateTagZero
  80. }
  81. // Defines the maximum number of streams the sender of this INIT
  82. // chunk allows the peer end to create in this association. The
  83. // value 0 MUST NOT be used.
  84. //
  85. // Note: There is no negotiation of the actual number of streams but
  86. // instead the two endpoints will use the min(requested, offered).
  87. // See Section 5.1.1 for details.
  88. //
  89. // Note: A receiver of an INIT with the MIS value of 0 SHOULD abort
  90. // the association.
  91. if i.numInboundStreams == 0 {
  92. abort = true
  93. return abort, ErrInitInboundStreamRequestZero
  94. }
  95. // Defines the number of outbound streams the sender of this INIT
  96. // chunk wishes to create in this association. The value of 0 MUST
  97. // NOT be used.
  98. //
  99. // Note: A receiver of an INIT with the OS value set to 0 SHOULD
  100. // abort the association.
  101. if i.numOutboundStreams == 0 {
  102. abort = true
  103. return abort, ErrInitOutboundStreamRequestZero
  104. }
  105. // An SCTP receiver MUST be able to receive a minimum of 1500 bytes in
  106. // one SCTP packet. This means that an SCTP endpoint MUST NOT indicate
  107. // less than 1500 bytes in its initial a_rwnd sent in the INIT or INIT
  108. // ACK.
  109. if i.advertisedReceiverWindowCredit < 1500 {
  110. abort = true
  111. return abort, ErrInitAdvertisedReceiver1500
  112. }
  113. return false, nil
  114. }
  115. // String makes chunkInit printable
  116. func (i *chunkInit) String() string {
  117. return fmt.Sprintf("%s\n%s", i.chunkHeader, i.chunkInitCommon)
  118. }