chunk_init.go 4.6 KB

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