-
Notifications
You must be signed in to change notification settings - Fork 0
/
icmp.go
306 lines (277 loc) · 9.32 KB
/
icmp.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
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
/*
MIT License
Copyright (c) 2017 Simon Schmidt
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package icmp
import "github.com/maxymania/ipsolution/ip"
import "github.com/maxymania/ipsolution/eth"
import "github.com/google/gopacket"
import "github.com/google/gopacket/layers"
import "fmt"
import "net"
import "container/list"
var ENotSupp = fmt.Errorf("Protocol not supported")
var EInvalid = fmt.Errorf("Protocol violation")
type PacketOutput interface{
WritePacketData(data []byte) error
}
type Notifyable interface{
Notify(i interface{})
}
type IPUnreachable struct{
FailType layers.ICMPv4TypeCode
Addr net.IP
}
type IP6Unreachable struct{
FailType layers.ICMPv6TypeCode
Addr net.IP
}
type IPProtocolControlMessage struct{
Protocol layers.IPProtocol
RemoteIP, LocalIP net.IP
RemotePort, LocalPort uint16
FailType layers.ICMPv4TypeCode
}
type Echo struct {
Head,Body []byte
Addr net.IP
}
type Host struct{
NetN Notifyable
NetNv6 Notifyable
EchoSocket Notifyable
NC6 *Nd6Cache
ARP *ArpCache
Host *ip.IPHost
Mac net.HardwareAddr
Vlan uint16
/* IPv6 */
CurHopLimit uint8
BaseReachableTime, ReachableTime uint32
IPv6MTU uint32
RetransTimer uint32
}
func copymac(i net.HardwareAddr) net.HardwareAddr {
j := make(net.HardwareAddr,len(i)); copy(j,i)
return j
}
func copyip(i net.IP) net.IP {
j := make(net.IP,len(i)); copy(j,i)
return j
}
func copydat(i []byte) []byte {
j := make([]byte,len(i)); copy(j,i)
return j
}
func duV6ToV4(code layers.ICMPv6TypeCode) layers.ICMPv4TypeCode {
var itc4 uint8
switch code.Code() {
case layers.ICMPv6CodeNoRouteToDst,
layers.ICMPv6CodeRejectRouteToDst,
layers.ICMPv6CodeSrcAddressFailedPolicy:
itc4 = layers.ICMPv4CodeSourceRoutingFailed
case layers.ICMPv6CodeBeyondScopeOfSrc:
itc4 = layers.ICMPv4CodeNet
case layers.ICMPv6CodeAdminProhibited:
/*
* ICMPv6 is less concise about what is prohibited or not.
* In this case, we must simply choose a sane default.
*
* We assume, that the Network is Administratively Prohibitet.
* Note, that this message is handled within the local host only.
*/
itc4 = layers.ICMPv4CodeNetAdminProhibited
case layers.ICMPv6CodeAddressUnreachable:
itc4 = layers.ICMPv4CodeHost /* Host Unreachable */
default:
itc4 = layers.ICMPv4CodeNet
}
return layers.CreateICMPv4TypeCode(layers.ICMPv4TypeDestinationUnreachable,itc4)
}
func (h *Host) Input(e *eth.EthLayer2, i *ip.IPLayerPart, po PacketOutput) error {
if i.IsAR {
h.arp(i,po)
return nil
}
switch i.NextLayerType {
case layers.LayerTypeICMPv4:
// ICMPv4 must not be in IPv6 packet
if i.IsV6 { return EInvalid }
return h.input4(e,i,po)
case layers.LayerTypeICMPv6:
// ICMPv6 must be in IPv6 packet
if !i.IsV6 { return EInvalid }
return h.input6(e,i,po)
}
return ENotSupp
}
// ICMPv4 input function
func (h *Host) input4(e *eth.EthLayer2, i *ip.IPLayerPart, po PacketOutput) (err error) {
var icmp layers.ICMPv4
err = icmp.DecodeFromBytes(i.Payload,gopacket.NilDecodeFeedback); if err!=nil { return }
switch icmp.TypeCode.Type() {
case layers.ICMPv4TypeEchoRequest:
icmp.TypeCode = layers.CreateICMPv4TypeCode(layers.ICMPv4TypeEchoReply,icmp.TypeCode.Code())
// Flip addresses.
i.V4.SrcIP,i.V4.DstIP = i.V4.DstIP,i.V4.SrcIP
e.SrcMAC,e.DstMAC = h.Mac,e.SrcMAC
e.VLANIdentifier = h.Vlan
// rewrite IPv4 fields.
i.V4.TOS = 0
i.V4.TTL = 64
so := gopacket.SerializeOptions{true,true}
sb := gopacket.NewSerializeBufferExpectedSize(len(icmp.Payload)+128,0)
gopacket.SerializeLayers(sb,so,e,&i.V4,&icmp,gopacket.Payload(icmp.Payload))
po.WritePacketData(sb.Bytes())
case layers.ICMPv4TypeEchoReply:
if h.EchoSocket==nil { return }
h.EchoSocket.Notify(&Echo{copydat(icmp.Contents),copydat(icmp.Payload),copyip(i.SrcIP)})
case layers.ICMPv4TypeDestinationUnreachable:
if h.NetN==nil { return }
switch icmp.TypeCode.Code() {
case layers.ICMPv4CodeProtocol,layers.ICMPv4CodePort:
ipProtocolControl := new(IPProtocolControlMessage)
if ipProtocolControl.decode(icmp.Payload,false,gopacket.NilDecodeFeedback)==nil {
ipProtocolControl.FailType = icmp.TypeCode
h.NetN.Notify(ipProtocolControl)
}
return
default:
h.NetN.Notify(&IPUnreachable{icmp.TypeCode,copyip(i.SrcIP)})
}
case layers.ICMPv4TypeTimeExceeded:
if h.NetN==nil { return }
switch icmp.TypeCode.Code() {
case layers.ICMPv4CodeTTLExceeded:
h.NetN.Notify(&IPUnreachable{icmp.TypeCode,copyip(i.SrcIP)})
}
case layers.ICMPv4TypeSourceQuench:
if h.NetN==nil { return }
h.NetN.Notify(&IPUnreachable{icmp.TypeCode,copyip(i.SrcIP)})
// TODO: many ICMP requests are still ignored, harvest as needed.
}
return
}
// ICMPv6 input function
func (h *Host) input6(e *eth.EthLayer2, i *ip.IPLayerPart, po PacketOutput) (err error) {
var icmp layers.ICMPv6
err = icmp.DecodeFromBytes(i.Payload,gopacket.NilDecodeFeedback); if err!=nil { return }
switch icmp.TypeCode.Type() {
case layers.ICMPv6TypeEchoRequest:
icmp.TypeCode = layers.CreateICMPv6TypeCode(layers.ICMPv6TypeEchoReply,icmp.TypeCode.Code())
// Flip addresses.
i.V6.SrcIP,i.V6.DstIP = i.V6.DstIP,i.V6.SrcIP
e.SrcMAC,e.DstMAC = e.DstMAC,e.SrcMAC
// rewrite IPv6 fields.
i.V6.TrafficClass = 0
i.V6.HopLimit = 64
so := gopacket.SerializeOptions{true,true}
sb := gopacket.NewSerializeBufferExpectedSize(len(icmp.Payload)+128,0)
gopacket.SerializeLayers(sb,so,e,&i.V6,&icmp,gopacket.Payload(icmp.Payload))
po.WritePacketData(sb.Bytes())
case layers.ICMPv6TypeEchoReply:
if h.EchoSocket==nil { return }
h.EchoSocket.Notify(&Echo{copydat(icmp.Contents),copydat(icmp.Payload),copyip(i.SrcIP)})
case layers.ICMPv6TypeDestinationUnreachable:
switch icmp.TypeCode.Code() {
case layers.ICMPv6CodePortUnreachable:
if h.NetN==nil { return }
ipProtocolControl := new(IPProtocolControlMessage)
if ipProtocolControl.decode(icmp.Payload,true,gopacket.NilDecodeFeedback)==nil {
ipProtocolControl.FailType = layers.CreateICMPv4TypeCode(
layers.ICMPv4TypeDestinationUnreachable,
layers.ICMPv4CodePort)
h.NetN.Notify(ipProtocolControl)
}
return
default:
if h.NetNv6!=nil {
h.NetNv6.Notify(IP6Unreachable{icmp.TypeCode,copyip(i.SrcIP)})
}else if h.NetN!=nil {
h.NetN.Notify(&IPUnreachable{duV6ToV4(icmp.TypeCode),copyip(i.SrcIP)})
}
}
case layers.ICMPv6TypeTimeExceeded:
if h.NetN==nil { return }
switch icmp.TypeCode.Code() {
case layers.ICMPv6CodeHopLimitExceeded:
h.NetN.Notify(&IPUnreachable{
layers.CreateICMPv4TypeCode(
layers.ICMPv4TypeTimeExceeded,
layers.ICMPv4CodeTTLExceeded),
copyip(i.SrcIP)})
}
case layers.ICMPv6TypePacketTooBig:
if h.NetN==nil { return }
h.NetN.Notify(&IPUnreachable{
layers.CreateICMPv4TypeCode(
layers.ICMPv4TypeDestinationUnreachable,
layers.ICMPv4CodeFragmentationNeeded),
copyip(i.SrcIP)})
case layers.ICMPv6TypeNeighborSolicitation:
if h.NC6==nil { return }
h.nd6NeighborSolicitation(i,&icmp,po)
case layers.ICMPv6TypeNeighborAdvertisement:
if h.NC6==nil { return }
h.nd6NeighborAdvertisement(i,&icmp,po)
case layers.ICMPv6TypeRouterAdvertisement:
if h.NC6==nil { return }
h.nd6RouterAdvertisement(i,&icmp,po)
case layers.ICMPv6TypeRedirect:
if h.NC6==nil { return }
h.nd6Redirect(i,&icmp,po)
case layers.ICMPv6TypeParameterProblem:
switch icmp.TypeCode.Code() {
case layers.ICMPv6CodeUnrecognizedNextHeader:
if h.NetN==nil { return }
ipProtocolControl := new(IPProtocolControlMessage)
if ipProtocolControl.decode(icmp.Payload,true,gopacket.NilDecodeFeedback)==nil {
ipProtocolControl.FailType = layers.CreateICMPv4TypeCode(
layers.ICMPv4TypeDestinationUnreachable,
layers.ICMPv4CodeProtocol)
h.NetN.Notify(ipProtocolControl)
}
return
}
// TODO: many ICMP requests are still ignored, harvest as needed.
}
return
}
func (h *Host) send(l *list.List, dst net.HardwareAddr, po PacketOutput, etype layers.EthernetType) {
if l.Len()==0 { return }
var e eth.EthLayer2
e.SrcMAC = h.Mac
e.VLANIdentifier = h.Vlan
e.DstMAC = dst
e.EthernetType = etype
op := gopacket.SerializeOptions{true,true}
for elem := l.Front(); elem!=nil; elem = elem.Next() {
switch ev := elem.Value.(type) {
case gopacket.SerializeBuffer:
if e.SerializeTo(ev,op)==nil {
po.WritePacketData(ev.Bytes())
}
case []byte:
ob := gopacket.NewSerializeBufferExpectedSize(len(ev)+20,0)
err := gopacket.SerializeLayers(ob,op,&e,gopacket.Payload(ev))
if err==nil {
po.WritePacketData(ob.Bytes())
}
}
}
}