chunk_init.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. var (
  23. errChunkTypeNotTypeInit = errors.New("ChunkType is not of type INIT")
  24. errChunkValueNotLongEnough = errors.New("chunk Value isn't long enough for mandatory parameters exp")
  25. errChunkTypeInitFlagZero = errors.New("ChunkType of type INIT flags must be all 0")
  26. errChunkTypeInitUnmarshalFailed = errors.New("failed to unmarshal INIT body")
  27. errChunkTypeInitMarshalFailed = errors.New("failed marshaling INIT common data")
  28. errChunkTypeInitInitateTagZero = errors.New("ChunkType of type INIT ACK InitiateTag must not be 0")
  29. errInitInboundStreamRequestZero = errors.New("INIT ACK inbound stream request must be > 0")
  30. errInitOutboundStreamRequestZero = errors.New("INIT ACK outbound stream request must be > 0")
  31. errInitAdvertisedReceiver1500 = errors.New("INIT ACK Advertised Receiver Window Credit (a_rwnd) must be >= 1500")
  32. )
  33. func (i *chunkInit) unmarshal(raw []byte) error {
  34. if err := i.chunkHeader.unmarshal(raw); err != nil {
  35. return err
  36. }
  37. if i.typ != ctInit {
  38. return fmt.Errorf("%w: actually is %s", errChunkTypeNotTypeInit, i.typ.String())
  39. } else if len(i.raw) < initChunkMinLength {
  40. return fmt.Errorf("%w: %d actual: %d", errChunkValueNotLongEnough, initChunkMinLength, len(i.raw))
  41. }
  42. // The Chunk Flags field in INIT is reserved, and all bits in it should
  43. // be set to 0 by the sender and ignored by the receiver. The sequence
  44. // of parameters within an INIT can be processed in any order.
  45. if i.flags != 0 {
  46. return errChunkTypeInitFlagZero
  47. }
  48. if err := i.chunkInitCommon.unmarshal(i.raw); err != nil {
  49. return fmt.Errorf("%w: %v", errChunkTypeInitUnmarshalFailed, err)
  50. }
  51. return nil
  52. }
  53. func (i *chunkInit) marshal() ([]byte, error) {
  54. initShared, err := i.chunkInitCommon.marshal()
  55. if err != nil {
  56. return nil, fmt.Errorf("%w: %v", errChunkTypeInitMarshalFailed, err)
  57. }
  58. i.chunkHeader.typ = ctInit
  59. i.chunkHeader.raw = initShared
  60. return i.chunkHeader.marshal()
  61. }
  62. func (i *chunkInit) check() (abort bool, err error) {
  63. // The receiver of the INIT (the responding end) records the value of
  64. // the Initiate Tag parameter. This value MUST be placed into the
  65. // Verification Tag field of every SCTP packet that the receiver of
  66. // the INIT transmits within this association.
  67. //
  68. // The Initiate Tag is allowed to have any value except 0. See
  69. // Section 5.3.1 for more on the selection of the tag value.
  70. //
  71. // If the value of the Initiate Tag in a received INIT chunk is found
  72. // to be 0, the receiver MUST treat it as an error and close the
  73. // association by transmitting an ABORT.
  74. if i.initiateTag == 0 {
  75. abort = true
  76. return abort, errChunkTypeInitInitateTagZero
  77. }
  78. // Defines the maximum number of streams the sender of this INIT
  79. // chunk allows the peer end to create in this association. The
  80. // value 0 MUST NOT be used.
  81. //
  82. // Note: There is no negotiation of the actual number of streams but
  83. // instead the two endpoints will use the min(requested, offered).
  84. // See Section 5.1.1 for details.
  85. //
  86. // Note: A receiver of an INIT with the MIS value of 0 SHOULD abort
  87. // the association.
  88. if i.numInboundStreams == 0 {
  89. abort = true
  90. return abort, errInitInboundStreamRequestZero
  91. }
  92. // Defines the number of outbound streams the sender of this INIT
  93. // chunk wishes to create in this association. The value of 0 MUST
  94. // NOT be used.
  95. //
  96. // Note: A receiver of an INIT with the OS value set to 0 SHOULD
  97. // abort the association.
  98. if i.numOutboundStreams == 0 {
  99. abort = true
  100. return abort, errInitOutboundStreamRequestZero
  101. }
  102. // An SCTP receiver MUST be able to receive a minimum of 1500 bytes in
  103. // one SCTP packet. This means that an SCTP endpoint MUST NOT indicate
  104. // less than 1500 bytes in its initial a_rwnd sent in the INIT or INIT
  105. // ACK.
  106. if i.advertisedReceiverWindowCredit < 1500 {
  107. abort = true
  108. return abort, errInitAdvertisedReceiver1500
  109. }
  110. return false, nil
  111. }
  112. // String makes chunkInit printable
  113. func (i *chunkInit) String() string {
  114. return fmt.Sprintf("%s\n%s", i.chunkHeader, i.chunkInitCommon)
  115. }