Ejemplo n.º 1
0
func decodeSCTPChunkTypeUnknown(data []byte, p gopacket.PacketBuilder) error {
	sc := &SCTPUnknownChunkType{SCTPChunk: decodeSCTPChunk(data)}
	sc.bytes = data[:sc.ActualLength]
	p.AddLayer(sc)
	p.SetErrorLayer(sc)
	return p.NextDecoder(gopacket.DecodeFunc(decodeWithSCTPChunkTypePrefix))
}
Ejemplo n.º 2
0
func decodeSCTPEmptyLayer(data []byte, p gopacket.PacketBuilder) error {
	sc := &SCTPEmptyLayer{
		SCTPChunk: decodeSCTPChunk(data),
	}
	p.AddLayer(sc)
	return p.NextDecoder(gopacket.DecodeFunc(decodeWithSCTPChunkTypePrefix))
}
Ejemplo n.º 3
0
func decodeSCTPShutdown(data []byte, p gopacket.PacketBuilder) error {
	sc := &SCTPShutdown{
		SCTPChunk:        decodeSCTPChunk(data),
		CumulativeTSNAck: binary.BigEndian.Uint32(data[4:8]),
	}
	p.AddLayer(sc)
	return p.NextDecoder(gopacket.DecodeFunc(decodeWithSCTPChunkTypePrefix))
}
Ejemplo n.º 4
0
func decodeSCTPCookieEcho(data []byte, p gopacket.PacketBuilder) error {
	sc := &SCTPCookieEcho{
		SCTPChunk: decodeSCTPChunk(data),
	}
	sc.Cookie = data[4:sc.Length]
	p.AddLayer(sc)
	return p.NextDecoder(gopacket.DecodeFunc(decodeWithSCTPChunkTypePrefix))
}
Ejemplo n.º 5
0
func decodeICMPv4(data []byte, p gopacket.PacketBuilder) error {
	i := &ICMPv4{}
	err := i.DecodeFromBytes(data, p)
	if err != nil {
		return err
	}
	p.AddLayer(i)
	return p.NextDecoder(gopacket.LayerTypePayload)
}
Ejemplo n.º 6
0
func decodeLinuxSLL(data []byte, p gopacket.PacketBuilder) error {
	sll := &LinuxSLL{}
	if err := sll.DecodeFromBytes(data, p); err != nil {
		return err
	}
	p.AddLayer(sll)
	p.SetLinkLayer(sll)
	return p.NextDecoder(sll.EthernetType)
}
Ejemplo n.º 7
0
func decodeIPv6HopByHop(data []byte, p gopacket.PacketBuilder) error {
	i := &IPv6HopByHop{}
	err := i.DecodeFromBytes(data, p)
	p.AddLayer(i)
	if err != nil {
		return err
	}
	return p.NextDecoder(i.NextHeader)
}
Ejemplo n.º 8
0
func decodeDHCPv4(data []byte, p gopacket.PacketBuilder) error {
	dhcp := &DHCPv4{}
	err := dhcp.DecodeFromBytes(data, p)
	if err != nil {
		return err
	}
	p.AddLayer(dhcp)
	return p.NextDecoder(gopacket.LayerTypePayload)
}
Ejemplo n.º 9
0
func decodeIPSecESP(data []byte, p gopacket.PacketBuilder) error {
	i := &IPSecESP{
		BaseLayer: BaseLayer{data, nil},
		SPI:       binary.BigEndian.Uint32(data[:4]),
		Seq:       binary.BigEndian.Uint32(data[4:8]),
		Encrypted: data[8:],
	}
	p.AddLayer(i)
	return nil
}
Ejemplo n.º 10
0
func decodeEthernet(data []byte, p gopacket.PacketBuilder) error {
	eth := &Ethernet{}
	err := eth.DecodeFromBytes(data, p)
	if err != nil {
		return err
	}
	p.AddLayer(eth)
	p.SetLinkLayer(eth)
	return p.NextDecoder(eth.EthernetType)
}
Ejemplo n.º 11
0
func decodeUDP(data []byte, p gopacket.PacketBuilder) error {
	udp := &UDP{}
	err := udp.DecodeFromBytes(data, p)
	p.AddLayer(udp)
	p.SetTransportLayer(udp)
	if err != nil {
		return err
	}
	return p.NextDecoder(udp.NextLayerType())
}
Ejemplo n.º 12
0
// decodeDNS decodes the byte slice into a DNS type. It also
// setups the application Layer in PacketBuilder.
func decodeDNS(data []byte, p gopacket.PacketBuilder) error {
	d := &DNS{}
	err := d.DecodeFromBytes(data, p)
	if err != nil {
		return err
	}
	p.AddLayer(d)
	p.SetApplicationLayer(d)
	return nil
}
Ejemplo n.º 13
0
func decodeIPv4(data []byte, p gopacket.PacketBuilder) error {
	ip := &IPv4{}
	err := ip.DecodeFromBytes(data, p)
	p.AddLayer(ip)
	p.SetNetworkLayer(ip)
	if err != nil {
		return err
	}
	return p.NextDecoder(ip.NextLayerType())
}
Ejemplo n.º 14
0
func decodeTCP(data []byte, p gopacket.PacketBuilder) error {
	tcp := &TCP{}
	err := tcp.DecodeFromBytes(data, p)
	p.AddLayer(tcp)
	p.SetTransportLayer(tcp)
	if err != nil {
		return err
	}
	return p.NextDecoder(gopacket.LayerTypePayload)
}
Ejemplo n.º 15
0
func decodeEthernetCTP(data []byte, p gopacket.PacketBuilder) error {
	c := &EthernetCTP{
		SkipCount: binary.LittleEndian.Uint16(data[:2]),
		BaseLayer: BaseLayer{data[:2], data[2:]},
	}
	if c.SkipCount%2 != 0 {
		return fmt.Errorf("EthernetCTP skip count is odd: %d", c.SkipCount)
	}
	p.AddLayer(c)
	return p.NextDecoder(gopacket.DecodeFunc(decodeEthernetCTPFromFunctionType))
}
Ejemplo n.º 16
0
func decodingLayerDecoder(d layerDecodingLayer, data []byte, p gopacket.PacketBuilder) error {
	err := d.DecodeFromBytes(data, p)
	if err != nil {
		return err
	}
	p.AddLayer(d)
	next := d.NextLayerType()
	if next == gopacket.LayerTypeZero {
		return nil
	}
	return p.NextDecoder(next)
}
Ejemplo n.º 17
0
// decodePPPoE decodes the PPPoE header (see http://tools.ietf.org/html/rfc2516).
func decodePPPoE(data []byte, p gopacket.PacketBuilder) error {
	pppoe := &PPPoE{
		Version:   data[0] >> 4,
		Type:      data[0] & 0x0F,
		Code:      PPPoECode(data[1]),
		SessionId: binary.BigEndian.Uint16(data[2:4]),
		Length:    binary.BigEndian.Uint16(data[4:6]),
	}
	pppoe.BaseLayer = BaseLayer{data[:6], data[6 : 6+pppoe.Length]}
	p.AddLayer(pppoe)
	return p.NextDecoder(pppoe.Code)
}
Ejemplo n.º 18
0
func decodeSNAP(data []byte, p gopacket.PacketBuilder) error {
	s := &SNAP{
		OrganizationalCode: data[:3],
		Type:               EthernetType(binary.BigEndian.Uint16(data[3:5])),
		BaseLayer:          BaseLayer{data[:5], data[5:]},
	}
	p.AddLayer(s)
	// BUG(gconnell):  When decoding SNAP, we treat the SNAP type as an Ethernet
	// type.  This may not actually be an ethernet type in all cases,
	// depending on the organizational code.  Right now, we don't check.
	return p.NextDecoder(s.Type)
}
Ejemplo n.º 19
0
func decodeFDDI(data []byte, p gopacket.PacketBuilder) error {
	f := &FDDI{
		FrameControl: FDDIFrameControl(data[0] & 0xF8),
		Priority:     data[0] & 0x07,
		SrcMAC:       net.HardwareAddr(data[1:7]),
		DstMAC:       net.HardwareAddr(data[7:13]),
		BaseLayer:    BaseLayer{data[:13], data[13:]},
	}
	p.SetLinkLayer(f)
	p.AddLayer(f)
	return p.NextDecoder(f.FrameControl)
}
Ejemplo n.º 20
0
func decodeSCTPHeartbeat(data []byte, p gopacket.PacketBuilder) error {
	sc := &SCTPHeartbeat{
		SCTPChunk: decodeSCTPChunk(data),
	}
	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))
}
Ejemplo n.º 21
0
func decodeIPv6Fragment(data []byte, p gopacket.PacketBuilder) error {
	i := &IPv6Fragment{
		BaseLayer:      BaseLayer{data[:8], data[8:]},
		NextHeader:     IPProtocol(data[0]),
		Reserved1:      data[1],
		FragmentOffset: binary.BigEndian.Uint16(data[2:4]) >> 3,
		Reserved2:      data[3] & 0x6 >> 1,
		MoreFragments:  data[3]&0x1 != 0,
		Identification: binary.BigEndian.Uint32(data[4:8]),
	}
	p.AddLayer(i)
	return p.NextDecoder(gopacket.DecodeFragment)
}
Ejemplo n.º 22
0
func decodeUDPLite(data []byte, p gopacket.PacketBuilder) error {
	udp := &UDPLite{
		SrcPort:          UDPLitePort(binary.BigEndian.Uint16(data[0:2])),
		sPort:            data[0:2],
		DstPort:          UDPLitePort(binary.BigEndian.Uint16(data[2:4])),
		dPort:            data[2:4],
		ChecksumCoverage: binary.BigEndian.Uint16(data[4:6]),
		Checksum:         binary.BigEndian.Uint16(data[6:8]),
		BaseLayer:        BaseLayer{data[:8], data[8:]},
	}
	p.AddLayer(udp)
	p.SetTransportLayer(udp)
	return p.NextDecoder(gopacket.LayerTypePayload)
}
Ejemplo n.º 23
0
func decodeSCTP(data []byte, p gopacket.PacketBuilder) error {
	sctp := &SCTP{
		SrcPort:         SCTPPort(binary.BigEndian.Uint16(data[:2])),
		sPort:           data[:2],
		DstPort:         SCTPPort(binary.BigEndian.Uint16(data[2:4])),
		dPort:           data[2:4],
		VerificationTag: binary.BigEndian.Uint32(data[4:8]),
		Checksum:        binary.BigEndian.Uint32(data[8:12]),
		BaseLayer:       BaseLayer{data[:12], data[12:]},
	}
	p.AddLayer(sctp)
	p.SetTransportLayer(sctp)
	return p.NextDecoder(sctpChunkTypePrefixDecoder)
}
Ejemplo n.º 24
0
func decodeNortelDiscovery(data []byte, p gopacket.PacketBuilder) error {
	c := &NortelDiscovery{}
	if len(data) < 11 {
		return fmt.Errorf("Invalid NortelDiscovery packet length %d", len(data))
	}
	c.IPAddress = data[0:4]
	c.SegmentID = data[4:7]
	c.Chassis = NDPChassisType(data[7])
	c.Backplane = NDPBackplaneType(data[8])
	c.State = NDPState(data[9])
	c.NumLinks = uint8(data[10])
	p.AddLayer(c)
	return nil
}
Ejemplo n.º 25
0
func decodeIPv6Destination(data []byte, p gopacket.PacketBuilder) error {
	i := &IPv6Destination{
		ipv6ExtensionBase: decodeIPv6ExtensionBase(data),
		// We guess we'll 1-2 options, one regular option at least, then maybe one
		// padding option.
		Options: make([]IPv6DestinationOption, 0, 2),
	}
	var opt *IPv6DestinationOption
	for d := i.Contents[2:]; len(d) > 0; d = d[opt.ActualLength:] {
		i.Options = append(i.Options, IPv6DestinationOption(decodeIPv6HeaderTLVOption(d)))
		opt = &i.Options[len(i.Options)-1]
	}
	p.AddLayer(i)
	return p.NextDecoder(i.NextHeader)
}
Ejemplo n.º 26
0
func decodeMPLS(data []byte, p gopacket.PacketBuilder) error {
	decoded := binary.BigEndian.Uint32(data[:4])
	mpls := &MPLS{
		Label:        decoded >> 12,
		TrafficClass: uint8(decoded>>9) & 0x7,
		StackBottom:  decoded&0x100 != 0,
		TTL:          uint8(decoded),
		BaseLayer:    BaseLayer{data[:4], data[4:]},
	}
	p.AddLayer(mpls)
	if mpls.StackBottom {
		return p.NextDecoder(MPLSPayloadDecoder)
	}
	return p.NextDecoder(gopacket.DecodeFunc(decodeMPLS))
}
Ejemplo n.º 27
0
func decodeRUDP(data []byte, p gopacket.PacketBuilder) error {
	r := &RUDP{
		SYN:          data[0]&0x80 != 0,
		ACK:          data[0]&0x40 != 0,
		EACK:         data[0]&0x20 != 0,
		RST:          data[0]&0x10 != 0,
		NUL:          data[0]&0x08 != 0,
		Version:      data[0] & 0x3,
		HeaderLength: data[1],
		SrcPort:      RUDPPort(data[2]),
		DstPort:      RUDPPort(data[3]),
		DataLength:   binary.BigEndian.Uint16(data[4:6]),
		Seq:          binary.BigEndian.Uint32(data[6:10]),
		Ack:          binary.BigEndian.Uint32(data[10:14]),
		Checksum:     binary.BigEndian.Uint32(data[14:18]),
	}
	if r.HeaderLength < 9 {
		return fmt.Errorf("RUDP packet with too-short header length %d", r.HeaderLength)
	}
	hlen := int(r.HeaderLength) * 2
	r.Contents = data[:hlen]
	r.Payload = data[hlen : hlen+int(r.DataLength)]
	r.VariableHeaderArea = data[18:hlen]
	headerData := r.VariableHeaderArea
	switch {
	case r.SYN:
		if len(headerData) != 6 {
			return fmt.Errorf("RUDP packet invalid SYN header length: %d", len(headerData))
		}
		r.RUDPHeaderSYN = &RUDPHeaderSYN{
			MaxOutstandingSegments: binary.BigEndian.Uint16(headerData[:2]),
			MaxSegmentSize:         binary.BigEndian.Uint16(headerData[2:4]),
			OptionFlags:            binary.BigEndian.Uint16(headerData[4:6]),
		}
	case r.EACK:
		if len(headerData)%4 != 0 {
			return fmt.Errorf("RUDP packet invalid EACK header length: %d", len(headerData))
		}
		r.RUDPHeaderEACK = &RUDPHeaderEACK{make([]uint32, len(headerData)/4)}
		for i := 0; i < len(headerData); i += 4 {
			r.SeqsReceivedOK[i/4] = binary.BigEndian.Uint32(headerData[i : i+4])
		}
	}
	p.AddLayer(r)
	p.SetTransportLayer(r)
	return p.NextDecoder(gopacket.LayerTypePayload)
}
Ejemplo n.º 28
0
func decodeIPSecAH(data []byte, p gopacket.PacketBuilder) error {
	i := &IPSecAH{
		ipv6ExtensionBase: ipv6ExtensionBase{
			NextHeader:   IPProtocol(data[0]),
			HeaderLength: data[1],
		},
		Reserved: binary.BigEndian.Uint16(data[2:4]),
		SPI:      binary.BigEndian.Uint32(data[4:8]),
		Seq:      binary.BigEndian.Uint32(data[8:12]),
	}
	i.ActualLength = (int(i.HeaderLength) + 2) * 4
	i.AuthenticationData = data[12:i.ActualLength]
	i.Contents = data[:i.ActualLength]
	i.Payload = data[i.ActualLength:]
	p.AddLayer(i)
	return p.NextDecoder(i.NextHeader)
}
Ejemplo n.º 29
0
func decodeSCTPData(data []byte, p gopacket.PacketBuilder) error {
	sc := &SCTPData{
		SCTPChunk:       decodeSCTPChunk(data),
		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: binary.BigEndian.Uint32(data[12:16]),
	}
	// Length is the length in bytes of the data, INCLUDING the 16-byte header.
	sc.PayloadData = data[16:sc.Length]
	p.AddLayer(sc)
	p.SetApplicationLayer(sc)
	return p.NextDecoder(gopacket.DecodeFunc(decodeWithSCTPChunkTypePrefix))
}
Ejemplo n.º 30
0
func decodeSCTPInit(data []byte, p gopacket.PacketBuilder) error {
	sc := &SCTPInit{
		SCTPChunk:                      decodeSCTPChunk(data),
		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))
}