chunk_init.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. ErrInitUnknownParam = errors.New("INIT with unknown param")
  36. )
  37. func (i *chunkInit) unmarshal(raw []byte) error {
  38. if err := i.chunkHeader.unmarshal(raw); err != nil {
  39. return err
  40. }
  41. if i.typ != ctInit {
  42. return fmt.Errorf("%w: actually is %s", ErrChunkTypeNotTypeInit, i.typ.String())
  43. } else if len(i.raw) < initChunkMinLength {
  44. return fmt.Errorf("%w: %d actual: %d", ErrChunkValueNotLongEnough, initChunkMinLength, len(i.raw))
  45. }
  46. // The Chunk Flags field in INIT is reserved, and all bits in it should
  47. // be set to 0 by the sender and ignored by the receiver. The sequence
  48. // of parameters within an INIT can be processed in any order.
  49. if i.flags != 0 {
  50. return ErrChunkTypeInitFlagZero
  51. }
  52. if err := i.chunkInitCommon.unmarshal(i.raw); err != nil {
  53. return fmt.Errorf("%w: %v", ErrChunkTypeInitUnmarshalFailed, err) //nolint:errorlint
  54. }
  55. return nil
  56. }
  57. func (i *chunkInit) marshal() ([]byte, error) {
  58. initShared, err := i.chunkInitCommon.marshal()
  59. if err != nil {
  60. return nil, fmt.Errorf("%w: %v", ErrChunkTypeInitMarshalFailed, err) //nolint:errorlint
  61. }
  62. i.chunkHeader.typ = ctInit
  63. i.chunkHeader.raw = initShared
  64. return i.chunkHeader.marshal()
  65. }
  66. func (i *chunkInit) check() (abort bool, err error) {
  67. // The receiver of the INIT (the responding end) records the value of
  68. // the Initiate Tag parameter. This value MUST be placed into the
  69. // Verification Tag field of every SCTP packet that the receiver of
  70. // the INIT transmits within this association.
  71. //
  72. // The Initiate Tag is allowed to have any value except 0. See
  73. // Section 5.3.1 for more on the selection of the tag value.
  74. //
  75. // If the value of the Initiate Tag in a received INIT chunk is found
  76. // to be 0, the receiver MUST treat it as an error and close the
  77. // association by transmitting an ABORT.
  78. if i.initiateTag == 0 {
  79. return true, 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. return true, ErrInitInboundStreamRequestZero
  93. }
  94. // Defines the number of outbound streams the sender of this INIT
  95. // chunk wishes to create in this association. The value of 0 MUST
  96. // NOT be used.
  97. //
  98. // Note: A receiver of an INIT with the OS value set to 0 SHOULD
  99. // abort the association.
  100. if i.numOutboundStreams == 0 {
  101. return true, ErrInitOutboundStreamRequestZero
  102. }
  103. // An SCTP receiver MUST be able to receive a minimum of 1500 bytes in
  104. // one SCTP packet. This means that an SCTP endpoint MUST NOT indicate
  105. // less than 1500 bytes in its initial a_rwnd sent in the INIT or INIT
  106. // ACK.
  107. if i.advertisedReceiverWindowCredit < 1500 {
  108. return true, ErrInitAdvertisedReceiver1500
  109. }
  110. for _, p := range i.unrecognizedParams {
  111. if p.unrecognizedAction == paramHeaderUnrecognizedActionStop ||
  112. p.unrecognizedAction == paramHeaderUnrecognizedActionStopAndReport {
  113. return true, ErrInitUnknownParam
  114. }
  115. }
  116. return false, nil
  117. }
  118. // String makes chunkInit printable
  119. func (i *chunkInit) String() string {
  120. return fmt.Sprintf("%s\n%s", i.chunkHeader, i.chunkInitCommon)
  121. }