forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
131 lines (116 loc) · 3.7 KB
/
message.go
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
package icmp
import (
"encoding/binary"
"time"
"github.com/elastic/beats/libbeat/logp"
"github.com/tsg/gopacket/layers"
)
// TODO: more types (that are not provided as constants in gopacket)
// ICMPv4 types that represent a response (all other types represent a request)
var icmp4ResponseTypes = map[uint8]bool{
layers.ICMPv4TypeEchoReply: true,
layers.ICMPv4TypeTimestampReply: true,
layers.ICMPv4TypeInfoReply: true,
layers.ICMPv4TypeAddressMaskReply: true,
}
// ICMPv6 types that represent a response (all other types represent a request)
var icmp6ResponseTypes = map[uint8]bool{
layers.ICMPv6TypeEchoReply: true,
}
// ICMPv4 types that represent an error
var icmp4ErrorTypes = map[uint8]bool{
layers.ICMPv4TypeDestinationUnreachable: true,
layers.ICMPv4TypeSourceQuench: true,
layers.ICMPv4TypeTimeExceeded: true,
layers.ICMPv4TypeParameterProblem: true,
}
// ICMPv6 types that represent an error
var icmp6ErrorTypes = map[uint8]bool{
layers.ICMPv6TypeDestinationUnreachable: true,
layers.ICMPv6TypePacketTooBig: true,
layers.ICMPv6TypeTimeExceeded: true,
layers.ICMPv6TypeParameterProblem: true,
}
// ICMPv4 types that require a request & a response
var icmp4PairTypes = map[uint8]bool{
layers.ICMPv4TypeEchoRequest: true,
layers.ICMPv4TypeEchoReply: true,
layers.ICMPv4TypeTimestampRequest: true,
layers.ICMPv4TypeTimestampReply: true,
layers.ICMPv4TypeInfoRequest: true,
layers.ICMPv4TypeInfoReply: true,
layers.ICMPv4TypeAddressMaskRequest: true,
layers.ICMPv4TypeAddressMaskReply: true,
}
// ICMPv6 types that require a request & a response
var icmp6PairTypes = map[uint8]bool{
layers.ICMPv6TypeEchoRequest: true,
layers.ICMPv6TypeEchoReply: true,
}
// Contains all used information from the ICMP message on the wire.
type icmpMessage struct {
ts time.Time
Type uint8
code uint8
length int
}
func isRequest(tuple *icmpTuple, msg *icmpMessage) bool {
if tuple.icmpVersion == 4 {
return !icmp4ResponseTypes[msg.Type]
}
if tuple.icmpVersion == 6 {
return !icmp6ResponseTypes[msg.Type]
}
logp.WTF("icmp", "Invalid ICMP version[%d]", tuple.icmpVersion)
return true
}
func isError(tuple *icmpTuple, msg *icmpMessage) bool {
if tuple.icmpVersion == 4 {
return icmp4ErrorTypes[msg.Type]
}
if tuple.icmpVersion == 6 {
return icmp6ErrorTypes[msg.Type]
}
logp.WTF("icmp", "Invalid ICMP version[%d]", tuple.icmpVersion)
return true
}
func requiresCounterpart(tuple *icmpTuple, msg *icmpMessage) bool {
if tuple.icmpVersion == 4 {
return icmp4PairTypes[msg.Type]
}
if tuple.icmpVersion == 6 {
return icmp6PairTypes[msg.Type]
}
logp.WTF("icmp", "Invalid ICMP version[%d]", tuple.icmpVersion)
return false
}
func extractTrackingData(icmpVersion uint8, msgType uint8, baseLayer *layers.BaseLayer) (uint16, uint16) {
if icmpVersion == 4 {
if icmp4PairTypes[msgType] {
id := binary.BigEndian.Uint16(baseLayer.Contents[4:6])
seq := binary.BigEndian.Uint16(baseLayer.Contents[6:8])
return id, seq
}
return 0, 0
}
if icmpVersion == 6 {
if icmp6PairTypes[msgType] {
id := binary.BigEndian.Uint16(baseLayer.Contents[4:6])
seq := binary.BigEndian.Uint16(baseLayer.Contents[6:8])
return id, seq
}
return 0, 0
}
logp.WTF("icmp", "Invalid ICMP version[%d]", icmpVersion)
return 0, 0
}
func humanReadable(tuple *icmpTuple, msg *icmpMessage) string {
if tuple.icmpVersion == 4 {
return layers.ICMPv4TypeCode(binary.BigEndian.Uint16([]byte{msg.Type, msg.code})).String()
}
if tuple.icmpVersion == 6 {
return layers.ICMPv6TypeCode(binary.BigEndian.Uint16([]byte{msg.Type, msg.code})).String()
}
logp.WTF("icmp", "Invalid ICMP version[%d]", tuple.icmpVersion)
return ""
}