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