1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
|
// Copyright 2012 Google, Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.
package layers
import (
"encoding/binary"
"errors"
"fmt"
"hash/crc32"
"github.com/gopacket/gopacket"
)
// SCTP contains information on the top level of an SCTP packet.
type SCTP struct {
BaseLayer
SrcPort, DstPort SCTPPort
VerificationTag uint32
Checksum uint32
sPort, dPort []byte
}
// LayerType returns gopacket.LayerTypeSCTP
func (s *SCTP) LayerType() gopacket.LayerType { return LayerTypeSCTP }
func decodeSCTP(data []byte, p gopacket.PacketBuilder) error {
sctp := &SCTP{}
err := sctp.DecodeFromBytes(data, p)
p.AddLayer(sctp)
p.SetTransportLayer(sctp)
if err != nil {
return err
}
return p.NextDecoder(sctpChunkTypePrefixDecoder)
}
var sctpChunkTypePrefixDecoder = gopacket.DecodeFunc(decodeWithSCTPChunkTypePrefix)
// TransportFlow returns a flow based on the source and destination SCTP port.
func (s *SCTP) TransportFlow() gopacket.Flow {
return gopacket.NewFlow(EndpointSCTPPort, s.sPort, s.dPort)
}
func decodeWithSCTPChunkTypePrefix(data []byte, p gopacket.PacketBuilder) error {
chunkType := SCTPChunkType(data[0])
return chunkType.Decode(data, p)
}
// SerializeTo is for gopacket.SerializableLayer.
func (s SCTP) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
bytes, err := b.PrependBytes(12)
if err != nil {
return err
}
binary.BigEndian.PutUint16(bytes[0:2], uint16(s.SrcPort))
binary.BigEndian.PutUint16(bytes[2:4], uint16(s.DstPort))
binary.BigEndian.PutUint32(bytes[4:8], s.VerificationTag)
if opts.ComputeChecksums {
// Note: MakeTable(Castagnoli) actually only creates the table once, then
// passes back a singleton on every other call, so this shouldn't cause
// excessive memory allocation.
binary.LittleEndian.PutUint32(bytes[8:12], crc32.Checksum(b.Bytes(), crc32.MakeTable(crc32.Castagnoli)))
}
return nil
}
func (sctp *SCTP) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
if len(data) < 12 {
return errors.New("Invalid SCTP common header length")
}
sctp.SrcPort = SCTPPort(binary.BigEndian.Uint16(data[:2]))
sctp.sPort = data[:2]
sctp.DstPort = SCTPPort(binary.BigEndian.Uint16(data[2:4]))
sctp.dPort = data[2:4]
sctp.VerificationTag = binary.BigEndian.Uint32(data[4:8])
sctp.Checksum = binary.BigEndian.Uint32(data[8:12])
sctp.BaseLayer = BaseLayer{data[:12], data[12:]}
return nil
}
func (t *SCTP) CanDecode() gopacket.LayerClass {
return LayerTypeSCTP
}
func (t *SCTP) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypePayload
}
// SCTPChunk contains the common fields in all SCTP chunks.
type SCTPChunk struct {
BaseLayer
Type SCTPChunkType
Flags uint8
Length uint16
// ActualLength is the total length of an SCTP chunk, including padding.
// SCTP chunks start and end on 4-byte boundaries. So if a chunk has a length
// of 18, it means that it has data up to and including byte 18, then padding
// up to the next 4-byte boundary, 20. In this case, Length would be 18, and
// ActualLength would be 20.
ActualLength int
}
func roundUpToNearest4(i int) int {
if i%4 == 0 {
return i
}
return i + 4 - (i % 4)
}
func decodeSCTPChunk(data []byte) (SCTPChunk, error) {
length := binary.BigEndian.Uint16(data[2:4])
if length < 4 {
return SCTPChunk{}, errors.New("invalid SCTP chunk length")
}
actual := roundUpToNearest4(int(length))
ct := SCTPChunkType(data[0])
// For SCTP Data, use a separate layer for the payload
delta := 0
if ct == SCTPChunkTypeData {
delta = int(actual) - int(length)
actual = 16
}
return SCTPChunk{
Type: ct,
Flags: data[1],
Length: length,
ActualLength: actual,
BaseLayer: BaseLayer{data[:actual], data[actual : len(data)-delta]},
}, nil
}
// SCTPParameter is a TLV parameter inside a SCTPChunk.
type SCTPParameter struct {
Type uint16
Length uint16
ActualLength int
Value []byte
}
func decodeSCTPParameter(data []byte) SCTPParameter {
length := binary.BigEndian.Uint16(data[2:4])
return SCTPParameter{
Type: binary.BigEndian.Uint16(data[0:2]),
Length: length,
Value: data[4:length],
ActualLength: roundUpToNearest4(int(length)),
}
}
func (p SCTPParameter) Bytes() []byte {
length := 4 + len(p.Value)
data := make([]byte, roundUpToNearest4(length))
binary.BigEndian.PutUint16(data[0:2], p.Type)
binary.BigEndian.PutUint16(data[2:4], uint16(length))
copy(data[4:], p.Value)
return data
}
// SCTPUnknownChunkType is the layer type returned when we don't recognize the
// chunk type. Since there's a length in a known location, we can skip over
// it even if we don't know what it is, and continue parsing the rest of the
// chunks. This chunk is stored as an ErrorLayer in the packet.
type SCTPUnknownChunkType struct {
SCTPChunk
bytes []byte
}
func decodeSCTPChunkTypeUnknown(data []byte, p gopacket.PacketBuilder) error {
chunk, err := decodeSCTPChunk(data)
if err != nil {
return err
}
sc := &SCTPUnknownChunkType{SCTPChunk: chunk}
sc.bytes = data[:sc.ActualLength]
p.AddLayer(sc)
p.SetErrorLayer(sc)
return p.NextDecoder(gopacket.DecodeFunc(decodeWithSCTPChunkTypePrefix))
}
// SerializeTo is for gopacket.SerializableLayer.
func (s SCTPUnknownChunkType) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
bytes, err := b.PrependBytes(s.ActualLength)
if err != nil {
return err
}
copy(bytes, s.bytes)
return nil
}
// LayerType returns gopacket.LayerTypeSCTPUnknownChunkType.
func (s *SCTPUnknownChunkType) LayerType() gopacket.LayerType { return LayerTypeSCTPUnknownChunkType }
// Payload returns all bytes in this header, including the decoded Type, Length,
// and Flags.
func (s *SCTPUnknownChunkType) Payload() []byte { return s.bytes }
// Error implements ErrorLayer.
func (s *SCTPUnknownChunkType) Error() error {
return fmt.Errorf("No decode method available for SCTP chunk type %s", s.Type)
}
// SCTPData is the SCTP Data chunk layer.
type SCTPData struct {
SCTPChunk
Unordered, BeginFragment, EndFragment bool
TSN uint32
StreamId uint16
StreamSequence uint16
PayloadProtocol SCTPPayloadProtocol
}
// LayerType returns gopacket.LayerTypeSCTPData.
func (s *SCTPData) LayerType() gopacket.LayerType { return LayerTypeSCTPData }
// SCTPPayloadProtocol represents a payload protocol
type SCTPPayloadProtocol uint32
// SCTPPayloadProtocol constonts from http://www.iana.org/assignments/sctp-parameters/sctp-parameters.xhtml
const (
SCTPProtocolReserved SCTPPayloadProtocol = 0
SCTPPayloadUIA = 1
SCTPPayloadM2UA = 2
SCTPPayloadM3UA = 3
SCTPPayloadSUA = 4
SCTPPayloadM2PA = 5
SCTPPayloadV5UA = 6
SCTPPayloadH248 = 7
SCTPPayloadBICC = 8
SCTPPayloadTALI = 9
SCTPPayloadDUA = 10
SCTPPayloadASAP = 11
SCTPPayloadENRP = 12
SCTPPayloadH323 = 13
SCTPPayloadQIPC = 14
SCTPPayloadSIMCO = 15
SCTPPayloadDDPSegment = 16
SCTPPayloadDDPStream = 17
SCTPPayloadS1AP = 18
)
func (p SCTPPayloadProtocol) String() string {
switch p {
case SCTPProtocolReserved:
return "Reserved"
case SCTPPayloadUIA:
return "UIA"
case SCTPPayloadM2UA:
return "M2UA"
case SCTPPayloadM3UA:
return "M3UA"
case SCTPPayloadSUA:
return "SUA"
case SCTPPayloadM2PA:
return "M2PA"
case SCTPPayloadV5UA:
return "V5UA"
case SCTPPayloadH248:
return "H.248"
case SCTPPayloadBICC:
return "BICC"
case SCTPPayloadTALI:
return "TALI"
case SCTPPayloadDUA:
return "DUA"
case SCTPPayloadASAP:
return "ASAP"
case SCTPPayloadENRP:
return "ENRP"
case SCTPPayloadH323:
return "H.323"
case SCTPPayloadQIPC:
return "QIPC"
case SCTPPayloadSIMCO:
return "SIMCO"
case SCTPPayloadDDPSegment:
return "DDPSegment"
case SCTPPayloadDDPStream:
return "DDPStream"
case SCTPPayloadS1AP:
return "S1AP"
}
return fmt.Sprintf("Unknown(%d)", p)
}
func decodeSCTPData(data []byte, p gopacket.PacketBuilder) error {
chunk, err := decodeSCTPChunk(data)
if err != nil {
return err
}
sc := &SCTPData{
SCTPChunk: chunk,
Unordered: data[1]&0x4 != 0,
BeginFragment: data[1]&0x2 != 0,
EndFragment: data[1]&0x1 != 0,
TSN: binary.BigEndian.Uint32(data[4:8]),
StreamId: binary.BigEndian.Uint16(data[8:10]),
StreamSequence: binary.BigEndian.Uint16(data[10:12]),
PayloadProtocol: SCTPPayloadProtocol(binary.BigEndian.Uint32(data[12:16])),
}
// Length is the length in bytes of the data, INCLUDING the 16-byte header.
p.AddLayer(sc)
return p.NextDecoder(gopacket.LayerTypePayload)
}
// SerializeTo is for gopacket.SerializableLayer.
func (sc SCTPData) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
payload := b.Bytes()
// Pad the payload to a 32 bit boundary
if rem := len(payload) % 4; rem != 0 {
b.AppendBytes(4 - rem)
}
length := 16
bytes, err := b.PrependBytes(length)
if err != nil {
return err
}
bytes[0] = uint8(sc.Type)
flags := uint8(0)
if sc.Unordered {
flags |= 0x4
}
if sc.BeginFragment {
flags |= 0x2
}
if sc.EndFragment {
flags |= 0x1
}
bytes[1] = flags
binary.BigEndian.PutUint16(bytes[2:4], uint16(length+len(payload)))
binary.BigEndian.PutUint32(bytes[4:8], sc.TSN)
binary.BigEndian.PutUint16(bytes[8:10], sc.StreamId)
binary.BigEndian.PutUint16(bytes[10:12], sc.StreamSequence)
binary.BigEndian.PutUint32(bytes[12:16], uint32(sc.PayloadProtocol))
return nil
}
// SCTPInitParameter is a parameter for an SCTP Init or InitAck packet.
type SCTPInitParameter SCTPParameter
// SCTPInit is used as the return value for both SCTPInit and SCTPInitAck
// messages.
type SCTPInit struct {
SCTPChunk
InitiateTag uint32
AdvertisedReceiverWindowCredit uint32
OutboundStreams, InboundStreams uint16
InitialTSN uint32
Parameters []SCTPInitParameter
}
// LayerType returns either gopacket.LayerTypeSCTPInit or gopacket.LayerTypeSCTPInitAck.
func (sc *SCTPInit) LayerType() gopacket.LayerType {
if sc.Type == SCTPChunkTypeInitAck {
return LayerTypeSCTPInitAck
}
// sc.Type == SCTPChunkTypeInit
return LayerTypeSCTPInit
}
func decodeSCTPInit(data []byte, p gopacket.PacketBuilder) error {
chunk, err := decodeSCTPChunk(data)
if err != nil {
return err
}
sc := &SCTPInit{
SCTPChunk: chunk,
InitiateTag: binary.BigEndian.Uint32(data[4:8]),
AdvertisedReceiverWindowCredit: binary.BigEndian.Uint32(data[8:12]),
OutboundStreams: binary.BigEndian.Uint16(data[12:14]),
InboundStreams: binary.BigEndian.Uint16(data[14:16]),
InitialTSN: binary.BigEndian.Uint32(data[16:20]),
}
paramData := data[20:sc.ActualLength]
for len(paramData) > 0 {
p := SCTPInitParameter(decodeSCTPParameter(paramData))
paramData = paramData[p.ActualLength:]
sc.Parameters = append(sc.Parameters, p)
}
p.AddLayer(sc)
return p.NextDecoder(gopacket.DecodeFunc(decodeWithSCTPChunkTypePrefix))
}
// SerializeTo is for gopacket.SerializableLayer.
func (sc SCTPInit) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
var payload []byte
for _, param := range sc.Parameters {
payload = append(payload, SCTPParameter(param).Bytes()...)
}
length := 20 + len(payload)
bytes, err := b.PrependBytes(roundUpToNearest4(length))
if err != nil {
return err
}
bytes[0] = uint8(sc.Type)
bytes[1] = sc.Flags
binary.BigEndian.PutUint16(bytes[2:4], uint16(length))
binary.BigEndian.PutUint32(bytes[4:8], sc.InitiateTag)
binary.BigEndian.PutUint32(bytes[8:12], sc.AdvertisedReceiverWindowCredit)
binary.BigEndian.PutUint16(bytes[12:14], sc.OutboundStreams)
binary.BigEndian.PutUint16(bytes[14:16], sc.InboundStreams)
binary.BigEndian.PutUint32(bytes[16:20], sc.InitialTSN)
copy(bytes[20:], payload)
return nil
}
// SCTPSack is the SCTP Selective ACK chunk layer.
type SCTPSack struct {
SCTPChunk
CumulativeTSNAck uint32
AdvertisedReceiverWindowCredit uint32
NumGapACKs, NumDuplicateTSNs uint16
GapACKs []uint16
DuplicateTSNs []uint32
}
// LayerType return LayerTypeSCTPSack
func (sc *SCTPSack) LayerType() gopacket.LayerType {
return LayerTypeSCTPSack
}
func decodeSCTPSack(data []byte, p gopacket.PacketBuilder) error {
chunk, err := decodeSCTPChunk(data)
if err != nil {
return err
}
sc := &SCTPSack{
SCTPChunk: chunk,
CumulativeTSNAck: binary.BigEndian.Uint32(data[4:8]),
AdvertisedReceiverWindowCredit: binary.BigEndian.Uint32(data[8:12]),
NumGapACKs: binary.BigEndian.Uint16(data[12:14]),
NumDuplicateTSNs: binary.BigEndian.Uint16(data[14:16]),
}
// We maximize gapAcks and dupTSNs here so we're not allocating tons
// of memory based on a user-controlable field. Our maximums are not exact,
// but should give us sane defaults... we'll still hit slice boundaries and
// fail if the user-supplied values are too high (in the for loops below), but
// the amount of memory we'll have allocated because of that should be small
// (< sc.ActualLength)
gapAcks := sc.SCTPChunk.ActualLength / 2
dupTSNs := (sc.SCTPChunk.ActualLength - gapAcks*2) / 4
if gapAcks > int(sc.NumGapACKs) {
gapAcks = int(sc.NumGapACKs)
}
if dupTSNs > int(sc.NumDuplicateTSNs) {
dupTSNs = int(sc.NumDuplicateTSNs)
}
sc.GapACKs = make([]uint16, 0, gapAcks)
sc.DuplicateTSNs = make([]uint32, 0, dupTSNs)
bytesRemaining := data[16:]
for i := 0; i < int(sc.NumGapACKs); i++ {
sc.GapACKs = append(sc.GapACKs, binary.BigEndian.Uint16(bytesRemaining[:2]))
bytesRemaining = bytesRemaining[2:]
}
for i := 0; i < int(sc.NumDuplicateTSNs); i++ {
sc.DuplicateTSNs = append(sc.DuplicateTSNs, binary.BigEndian.Uint32(bytesRemaining[:4]))
bytesRemaining = bytesRemaining[4:]
}
p.AddLayer(sc)
return p.NextDecoder(gopacket.DecodeFunc(decodeWithSCTPChunkTypePrefix))
}
// SerializeTo is for gopacket.SerializableLayer.
func (sc SCTPSack) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
length := 16 + 2*len(sc.GapACKs) + 4*len(sc.DuplicateTSNs)
bytes, err := b.PrependBytes(roundUpToNearest4(length))
if err != nil {
return err
}
bytes[0] = uint8(sc.Type)
bytes[1] = sc.Flags
binary.BigEndian.PutUint16(bytes[2:4], uint16(length))
binary.BigEndian.PutUint32(bytes[4:8], sc.CumulativeTSNAck)
binary.BigEndian.PutUint32(bytes[8:12], sc.AdvertisedReceiverWindowCredit)
binary.BigEndian.PutUint16(bytes[12:14], uint16(len(sc.GapACKs)))
binary.BigEndian.PutUint16(bytes[14:16], uint16(len(sc.DuplicateTSNs)))
for i, v := range sc.GapACKs {
binary.BigEndian.PutUint16(bytes[16+i*2:], v)
}
offset := 16 + 2*len(sc.GapACKs)
for i, v := range sc.DuplicateTSNs {
binary.BigEndian.PutUint32(bytes[offset+i*4:], v)
}
return nil
}
// SCTPHeartbeatParameter is the parameter type used by SCTP heartbeat and
// heartbeat ack layers.
type SCTPHeartbeatParameter SCTPParameter
// SCTPHeartbeat is the SCTP heartbeat layer, also used for heatbeat ack.
type SCTPHeartbeat struct {
SCTPChunk
Parameters []SCTPHeartbeatParameter
}
// LayerType returns gopacket.LayerTypeSCTPHeartbeat.
func (sc *SCTPHeartbeat) LayerType() gopacket.LayerType {
if sc.Type == SCTPChunkTypeHeartbeatAck {
return LayerTypeSCTPHeartbeatAck
}
// sc.Type == SCTPChunkTypeHeartbeat
return LayerTypeSCTPHeartbeat
}
func decodeSCTPHeartbeat(data []byte, p gopacket.PacketBuilder) error {
chunk, err := decodeSCTPChunk(data)
if err != nil {
return err
}
sc := &SCTPHeartbeat{
SCTPChunk: chunk,
}
paramData := data[4:sc.Length]
for len(paramData) > 0 {
p := SCTPHeartbeatParameter(decodeSCTPParameter(paramData))
paramData = paramData[p.ActualLength:]
sc.Parameters = append(sc.Parameters, p)
}
p.AddLayer(sc)
return p.NextDecoder(gopacket.DecodeFunc(decodeWithSCTPChunkTypePrefix))
}
// SerializeTo is for gopacket.SerializableLayer.
func (sc SCTPHeartbeat) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
var payload []byte
for _, param := range sc.Parameters {
payload = append(payload, SCTPParameter(param).Bytes()...)
}
length := 4 + len(payload)
bytes, err := b.PrependBytes(roundUpToNearest4(length))
if err != nil {
return err
}
bytes[0] = uint8(sc.Type)
bytes[1] = sc.Flags
binary.BigEndian.PutUint16(bytes[2:4], uint16(length))
copy(bytes[4:], payload)
return nil
}
// SCTPErrorParameter is the parameter type used by SCTP Abort and Error layers.
type SCTPErrorParameter SCTPParameter
// SCTPError is the SCTP error layer, also used for SCTP aborts.
type SCTPError struct {
SCTPChunk
Parameters []SCTPErrorParameter
}
// LayerType returns LayerTypeSCTPAbort or LayerTypeSCTPError.
func (sc *SCTPError) LayerType() gopacket.LayerType {
if sc.Type == SCTPChunkTypeAbort {
return LayerTypeSCTPAbort
}
// sc.Type == SCTPChunkTypeError
return LayerTypeSCTPError
}
func decodeSCTPError(data []byte, p gopacket.PacketBuilder) error {
// remarkably similar to decodeSCTPHeartbeat ;)
chunk, err := decodeSCTPChunk(data)
if err != nil {
return err
}
sc := &SCTPError{
SCTPChunk: chunk,
}
paramData := data[4:sc.Length]
for len(paramData) > 0 {
p := SCTPErrorParameter(decodeSCTPParameter(paramData))
paramData = paramData[p.ActualLength:]
sc.Parameters = append(sc.Parameters, p)
}
p.AddLayer(sc)
return p.NextDecoder(gopacket.DecodeFunc(decodeWithSCTPChunkTypePrefix))
}
// SerializeTo is for gopacket.SerializableLayer.
func (sc SCTPError) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
var payload []byte
for _, param := range sc.Parameters {
payload = append(payload, SCTPParameter(param).Bytes()...)
}
length := 4 + len(payload)
bytes, err := b.PrependBytes(roundUpToNearest4(length))
if err != nil {
return err
}
bytes[0] = uint8(sc.Type)
bytes[1] = sc.Flags
binary.BigEndian.PutUint16(bytes[2:4], uint16(length))
copy(bytes[4:], payload)
return nil
}
// SCTPShutdown is the SCTP shutdown layer.
type SCTPShutdown struct {
SCTPChunk
CumulativeTSNAck uint32
}
// LayerType returns gopacket.LayerTypeSCTPShutdown.
func (sc *SCTPShutdown) LayerType() gopacket.LayerType { return LayerTypeSCTPShutdown }
func decodeSCTPShutdown(data []byte, p gopacket.PacketBuilder) error {
chunk, err := decodeSCTPChunk(data)
if err != nil {
return err
}
sc := &SCTPShutdown{
SCTPChunk: chunk,
CumulativeTSNAck: binary.BigEndian.Uint32(data[4:8]),
}
p.AddLayer(sc)
return p.NextDecoder(gopacket.DecodeFunc(decodeWithSCTPChunkTypePrefix))
}
// SerializeTo is for gopacket.SerializableLayer.
func (sc SCTPShutdown) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
bytes, err := b.PrependBytes(8)
if err != nil {
return err
}
bytes[0] = uint8(sc.Type)
bytes[1] = sc.Flags
binary.BigEndian.PutUint16(bytes[2:4], 8)
binary.BigEndian.PutUint32(bytes[4:8], sc.CumulativeTSNAck)
return nil
}
// SCTPShutdownAck is the SCTP shutdown layer.
type SCTPShutdownAck struct {
SCTPChunk
}
// LayerType returns gopacket.LayerTypeSCTPShutdownAck.
func (sc *SCTPShutdownAck) LayerType() gopacket.LayerType { return LayerTypeSCTPShutdownAck }
func decodeSCTPShutdownAck(data []byte, p gopacket.PacketBuilder) error {
chunk, err := decodeSCTPChunk(data)
if err != nil {
return err
}
sc := &SCTPShutdownAck{
SCTPChunk: chunk,
}
p.AddLayer(sc)
return p.NextDecoder(gopacket.DecodeFunc(decodeWithSCTPChunkTypePrefix))
}
// SerializeTo is for gopacket.SerializableLayer.
func (sc SCTPShutdownAck) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
bytes, err := b.PrependBytes(4)
if err != nil {
return err
}
bytes[0] = uint8(sc.Type)
bytes[1] = sc.Flags
binary.BigEndian.PutUint16(bytes[2:4], 4)
return nil
}
// SCTPCookieEcho is the SCTP Cookie Echo layer.
type SCTPCookieEcho struct {
SCTPChunk
Cookie []byte
}
// LayerType returns gopacket.LayerTypeSCTPCookieEcho.
func (sc *SCTPCookieEcho) LayerType() gopacket.LayerType { return LayerTypeSCTPCookieEcho }
func decodeSCTPCookieEcho(data []byte, p gopacket.PacketBuilder) error {
chunk, err := decodeSCTPChunk(data)
if err != nil {
return err
}
sc := &SCTPCookieEcho{
SCTPChunk: chunk,
}
sc.Cookie = data[4:sc.Length]
p.AddLayer(sc)
return p.NextDecoder(gopacket.DecodeFunc(decodeWithSCTPChunkTypePrefix))
}
// SerializeTo is for gopacket.SerializableLayer.
func (sc SCTPCookieEcho) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
length := 4 + len(sc.Cookie)
bytes, err := b.PrependBytes(roundUpToNearest4(length))
if err != nil {
return err
}
bytes[0] = uint8(sc.Type)
bytes[1] = sc.Flags
binary.BigEndian.PutUint16(bytes[2:4], uint16(length))
copy(bytes[4:], sc.Cookie)
return nil
}
// This struct is used by all empty SCTP chunks (currently CookieAck and
// ShutdownComplete).
type SCTPEmptyLayer struct {
SCTPChunk
}
// LayerType returns either gopacket.LayerTypeSCTPShutdownComplete or
// LayerTypeSCTPCookieAck.
func (sc *SCTPEmptyLayer) LayerType() gopacket.LayerType {
if sc.Type == SCTPChunkTypeShutdownComplete {
return LayerTypeSCTPShutdownComplete
}
// sc.Type == SCTPChunkTypeCookieAck
return LayerTypeSCTPCookieAck
}
func decodeSCTPEmptyLayer(data []byte, p gopacket.PacketBuilder) error {
chunk, err := decodeSCTPChunk(data)
if err != nil {
return err
}
sc := &SCTPEmptyLayer{
SCTPChunk: chunk,
}
p.AddLayer(sc)
return p.NextDecoder(gopacket.DecodeFunc(decodeWithSCTPChunkTypePrefix))
}
// SerializeTo is for gopacket.SerializableLayer.
func (sc SCTPEmptyLayer) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
bytes, err := b.PrependBytes(4)
if err != nil {
return err
}
bytes[0] = uint8(sc.Type)
bytes[1] = sc.Flags
binary.BigEndian.PutUint16(bytes[2:4], 4)
return nil
}
|