| 1 |
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly> |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package sctp |
| 5 |
|
| 6 |
import ( |
| 7 |
"encoding/binary" |
| 8 |
"errors" |
| 9 |
"fmt" |
| 10 |
"strings" |
| 11 |
) |
| 12 |
|
| 13 |
/* |
| 14 |
chunkInitCommon represents an SCTP Chunk body of type INIT and INIT ACK |
| 15 |
|
| 16 |
0 1 2 3 |
| 17 |
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 18 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 19 |
| Type = 1 | Chunk Flags | Chunk Length | |
| 20 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 21 |
| Initiate Tag | |
| 22 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 23 |
| Advertised Receiver Window Credit (a_rwnd) | |
| 24 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 25 |
| Number of Outbound Streams | Number of Inbound Streams | |
| 26 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 27 |
| Initial TSN | |
| 28 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 29 |
| | |
| 30 |
| Optional/Variable-Length Parameters | |
| 31 |
| | |
| 32 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 33 |
|
| 34 |
The INIT chunk contains the following parameters. Unless otherwise |
| 35 |
noted, each parameter MUST only be included once in the INIT chunk. |
| 36 |
|
| 37 |
Fixed Parameters Status |
| 38 |
---------------------------------------------- |
| 39 |
Initiate Tag Mandatory |
| 40 |
Advertised Receiver Window Credit Mandatory |
| 41 |
Number of Outbound Streams Mandatory |
| 42 |
Number of Inbound Streams Mandatory |
| 43 |
Initial TSN Mandatory |
| 44 |
*/ |
| 45 |
|
| 46 |
type chunkInitCommon struct { |
| 47 |
initiateTag uint32 |
| 48 |
advertisedReceiverWindowCredit uint32 |
| 49 |
numOutboundStreams uint16 |
| 50 |
numInboundStreams uint16 |
| 51 |
initialTSN uint32 |
| 52 |
params []param |
| 53 |
unrecognizedParams []paramHeader |
| 54 |
} |
| 55 |
|
| 56 |
const ( |
| 57 |
initChunkMinLength = 16 |
| 58 |
initOptionalVarHeaderLength = 4 |
| 59 |
) |
| 60 |
|
| 61 |
// Init chunk errors. |
| 62 |
var ( |
| 63 |
ErrInitChunkParseParamTypeFailed = errors.New("failed to parse param type") |
| 64 |
ErrInitAckMarshalParam = errors.New("unable to marshal parameter for INIT/INITACK") |
| 65 |
) |
| 66 |
|
| 67 |
func (i *chunkInitCommon) unmarshal(raw []byte) error { |
| 68 |
i.initiateTag = binary.BigEndian.Uint32(raw[0:]) |
| 69 |
i.advertisedReceiverWindowCredit = binary.BigEndian.Uint32(raw[4:]) |
| 70 |
i.numOutboundStreams = binary.BigEndian.Uint16(raw[8:]) |
| 71 |
i.numInboundStreams = binary.BigEndian.Uint16(raw[10:]) |
| 72 |
i.initialTSN = binary.BigEndian.Uint32(raw[12:]) |
| 73 |
|
| 74 |
// https://tools.ietf.org/html/rfc4960#section-3.2.1 |
| 75 |
// |
| 76 |
// Chunk values of SCTP control chunks consist of a chunk-type-specific |
| 77 |
// header of required fields, followed by zero or more parameters. The |
| 78 |
// optional and variable-length parameters contained in a chunk are |
| 79 |
// defined in a Type-Length-Value format as shown below. |
| 80 |
// |
| 81 |
// 0 1 2 3 |
| 82 |
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 83 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 84 |
// | Parameter Type | Parameter Length | |
| 85 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 86 |
// | | |
| 87 |
// | Parameter Value | |
| 88 |
// | | |
| 89 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 90 |
|
| 91 |
offset := initChunkMinLength |
| 92 |
remaining := len(raw) - offset |
| 93 |
for remaining > 0 { |
| 94 |
if remaining > initOptionalVarHeaderLength { |
| 95 |
var pHeader paramHeader |
| 96 |
if err := pHeader.unmarshal(raw[offset:]); err != nil { |
| 97 |
return fmt.Errorf("%w: %v", ErrInitChunkParseParamTypeFailed, err) //nolint:errorlint |
| 98 |
} |
| 99 |
|
| 100 |
p, err := buildParam(pHeader.typ, raw[offset:]) |
| 101 |
if err != nil { |
| 102 |
i.unrecognizedParams = append(i.unrecognizedParams, pHeader) |
| 103 |
} else { |
| 104 |
i.params = append(i.params, p) |
| 105 |
} |
| 106 |
|
| 107 |
padding := getPadding(pHeader.length()) |
| 108 |
offset += pHeader.length() + padding |
| 109 |
remaining -= pHeader.length() + padding |
| 110 |
} else { |
| 111 |
break |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
return nil |
| 116 |
} |
| 117 |
|
| 118 |
func (i *chunkInitCommon) marshal() ([]byte, error) { |
| 119 |
out := make([]byte, initChunkMinLength) |
| 120 |
binary.BigEndian.PutUint32(out[0:], i.initiateTag) |
| 121 |
binary.BigEndian.PutUint32(out[4:], i.advertisedReceiverWindowCredit) |
| 122 |
binary.BigEndian.PutUint16(out[8:], i.numOutboundStreams) |
| 123 |
binary.BigEndian.PutUint16(out[10:], i.numInboundStreams) |
| 124 |
binary.BigEndian.PutUint32(out[12:], i.initialTSN) |
| 125 |
for idx, p := range i.params { |
| 126 |
pp, err := p.marshal() |
| 127 |
if err != nil { |
| 128 |
return nil, fmt.Errorf("%w: %v", ErrInitAckMarshalParam, err) //nolint:errorlint |
| 129 |
} |
| 130 |
|
| 131 |
out = append(out, pp...) //nolint:makezero // TODO: fix |
| 132 |
|
| 133 |
// Chunks (including Type, Length, and Value fields) are padded out |
| 134 |
// by the sender with all zero bytes to be a multiple of 4 bytes |
| 135 |
// long. This padding MUST NOT be more than 3 bytes in total. The |
| 136 |
// Chunk Length value does not include terminating padding of the |
| 137 |
// chunk. *However, it does include padding of any variable-length |
| 138 |
// parameter except the last parameter in the chunk.* The receiver |
| 139 |
// MUST ignore the padding. |
| 140 |
if idx != len(i.params)-1 { |
| 141 |
out = padByte(out, getPadding(len(pp))) |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
return out, nil |
| 146 |
} |
| 147 |
|
| 148 |
// String makes chunkInitCommon printable. |
| 149 |
func (i chunkInitCommon) String() string { |
| 150 |
format := `initiateTag: %d |
| 151 |
advertisedReceiverWindowCredit: %d |
| 152 |
numOutboundStreams: %d |
| 153 |
numInboundStreams: %d |
| 154 |
initialTSN: %d` |
| 155 |
|
| 156 |
var res strings.Builder |
| 157 |
fmt.Fprintf(&res, format, |
| 158 |
i.initiateTag, |
| 159 |
i.advertisedReceiverWindowCredit, |
| 160 |
i.numOutboundStreams, |
| 161 |
i.numInboundStreams, |
| 162 |
i.initialTSN, |
| 163 |
) |
| 164 |
|
| 165 |
for i, param := range i.params { |
| 166 |
fmt.Fprintf(&res, "Param %d:\n %s", i, param) |
| 167 |
} |
| 168 |
|
| 169 |
return res.String() |
| 170 |
} |
| 171 |
|
| 172 |
// allZero returns true if every byte is 0x00. |
| 173 |
func allZero(b []byte) bool { |
| 174 |
for _, v := range b { |
| 175 |
if v != 0 { |
| 176 |
return false |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
return true |
| 181 |
} |