forked from google/gopacket
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathicmp6msg.go
578 lines (475 loc) · 16.8 KB
/
icmp6msg.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
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
// Copyright 2012 Google, Inc. All rights reserved.
// Copyright 2009-2011 Andreas Krennmair. 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"
"encoding/hex"
"errors"
"fmt"
"net"
"time"
"github.com/kubeshark/gopacket"
)
// Based on RFC 4861
// ICMPv6Opt indicate how to decode the data associated with each ICMPv6Option.
type ICMPv6Opt uint8
const (
_ ICMPv6Opt = iota
// ICMPv6OptSourceAddress contains the link-layer address of the sender of
// the packet. It is used in the Neighbor Solicitation, Router
// Solicitation, and Router Advertisement packets. Must be ignored for other
// Neighbor discovery messages.
ICMPv6OptSourceAddress
// ICMPv6OptTargetAddress contains the link-layer address of the target. It
// is used in Neighbor Advertisement and Redirect packets. Must be ignored
// for other Neighbor discovery messages.
ICMPv6OptTargetAddress
// ICMPv6OptPrefixInfo provides hosts with on-link prefixes and prefixes
// for Address Autoconfiguration. The Prefix Information option appears in
// Router Advertisement packets and MUST be silently ignored for other
// messages.
ICMPv6OptPrefixInfo
// ICMPv6OptRedirectedHeader is used in Redirect messages and contains all
// or part of the packet that is being redirected.
ICMPv6OptRedirectedHeader
// ICMPv6OptMTU is used in Router Advertisement messages to ensure that all
// nodes on a link use the same MTU value in those cases where the link MTU
// is not well known. This option MUST be silently ignored for other
// Neighbor Discovery messages.
ICMPv6OptMTU
)
// ICMPv6Echo represents the structure of a ping.
type ICMPv6Echo struct {
BaseLayer
Identifier uint16
SeqNumber uint16
}
// ICMPv6RouterSolicitation is sent by hosts to find routers.
type ICMPv6RouterSolicitation struct {
BaseLayer
Options ICMPv6Options
}
// ICMPv6RouterAdvertisement is sent by routers in response to Solicitation.
type ICMPv6RouterAdvertisement struct {
BaseLayer
HopLimit uint8
Flags uint8
RouterLifetime uint16
ReachableTime uint32
RetransTimer uint32
Options ICMPv6Options
}
// ICMPv6NeighborSolicitation is sent to request the link-layer address of a
// target node.
type ICMPv6NeighborSolicitation struct {
BaseLayer
TargetAddress net.IP
Options ICMPv6Options
}
// ICMPv6NeighborAdvertisement is sent by nodes in response to Solicitation.
type ICMPv6NeighborAdvertisement struct {
BaseLayer
Flags uint8
TargetAddress net.IP
Options ICMPv6Options
}
// ICMPv6Redirect is sent by routers to inform hosts of a better first-hop node
// on the path to a destination.
type ICMPv6Redirect struct {
BaseLayer
TargetAddress net.IP
DestinationAddress net.IP
Options ICMPv6Options
}
// ICMPv6Option contains the type and data for a single option.
type ICMPv6Option struct {
Type ICMPv6Opt
Data []byte
}
// ICMPv6Options is a slice of ICMPv6Option.
type ICMPv6Options []ICMPv6Option
func (i ICMPv6Opt) String() string {
switch i {
case ICMPv6OptSourceAddress:
return "SourceAddress"
case ICMPv6OptTargetAddress:
return "TargetAddress"
case ICMPv6OptPrefixInfo:
return "PrefixInfo"
case ICMPv6OptRedirectedHeader:
return "RedirectedHeader"
case ICMPv6OptMTU:
return "MTU"
default:
return fmt.Sprintf("Unknown(%d)", i)
}
}
// CanDecode returns the set of layer types that this DecodingLayer can decode.
func (i *ICMPv6Echo) CanDecode() gopacket.LayerClass {
return LayerTypeICMPv6Echo
}
// LayerType returns LayerTypeICMPv6Echo.
func (i *ICMPv6Echo) LayerType() gopacket.LayerType {
return LayerTypeICMPv6Echo
}
// NextLayerType returns the layer type contained by this DecodingLayer.
func (i *ICMPv6Echo) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypePayload
}
// DecodeFromBytes decodes the given bytes into this layer.
func (i *ICMPv6Echo) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
if len(data) < 4 {
df.SetTruncated()
return errors.New("ICMP layer less then 4 bytes for ICMPv6 Echo")
}
i.Identifier = binary.BigEndian.Uint16(data[0:2])
i.SeqNumber = binary.BigEndian.Uint16(data[2:4])
return nil
}
// SerializeTo writes the serialized form of this layer into the
// SerializationBuffer, implementing gopacket.SerializableLayer.
// See the docs for gopacket.SerializableLayer for more info.
func (i *ICMPv6Echo) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
buf, err := b.PrependBytes(4)
if err != nil {
return err
}
binary.BigEndian.PutUint16(buf, i.Identifier)
binary.BigEndian.PutUint16(buf[2:], i.SeqNumber)
return nil
}
// LayerType returns LayerTypeICMPv6.
func (i *ICMPv6RouterSolicitation) LayerType() gopacket.LayerType {
return LayerTypeICMPv6RouterSolicitation
}
// NextLayerType returns the layer type contained by this DecodingLayer.
func (i *ICMPv6RouterSolicitation) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypePayload
}
// DecodeFromBytes decodes the given bytes into this layer.
func (i *ICMPv6RouterSolicitation) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
// first 4 bytes are reserved followed by options
if len(data) < 4 {
df.SetTruncated()
return errors.New("ICMP layer less then 4 bytes for ICMPv6 router solicitation")
}
// truncate old options
i.Options = i.Options[:0]
return i.Options.DecodeFromBytes(data[4:], df)
}
// SerializeTo writes the serialized form of this layer into the
// SerializationBuffer, implementing gopacket.SerializableLayer.
// See the docs for gopacket.SerializableLayer for more info.
func (i *ICMPv6RouterSolicitation) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
if err := i.Options.SerializeTo(b, opts); err != nil {
return err
}
buf, err := b.PrependBytes(4)
if err != nil {
return err
}
copy(buf, lotsOfZeros[:4])
return nil
}
// CanDecode returns the set of layer types that this DecodingLayer can decode.
func (i *ICMPv6RouterSolicitation) CanDecode() gopacket.LayerClass {
return LayerTypeICMPv6RouterSolicitation
}
// LayerType returns LayerTypeICMPv6RouterAdvertisement.
func (i *ICMPv6RouterAdvertisement) LayerType() gopacket.LayerType {
return LayerTypeICMPv6RouterAdvertisement
}
// NextLayerType returns the layer type contained by this DecodingLayer.
func (i *ICMPv6RouterAdvertisement) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypePayload
}
// DecodeFromBytes decodes the given bytes into this layer.
func (i *ICMPv6RouterAdvertisement) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
if len(data) < 12 {
df.SetTruncated()
return errors.New("ICMP layer less then 12 bytes for ICMPv6 router advertisement")
}
i.HopLimit = uint8(data[0])
// M, O bit followed by 6 reserved bits
i.Flags = uint8(data[1])
i.RouterLifetime = binary.BigEndian.Uint16(data[2:4])
i.ReachableTime = binary.BigEndian.Uint32(data[4:8])
i.RetransTimer = binary.BigEndian.Uint32(data[8:12])
i.BaseLayer = BaseLayer{data, nil} // assume no payload
// truncate old options
i.Options = i.Options[:0]
return i.Options.DecodeFromBytes(data[12:], df)
}
// SerializeTo writes the serialized form of this layer into the
// SerializationBuffer, implementing gopacket.SerializableLayer.
// See the docs for gopacket.SerializableLayer for more info.
func (i *ICMPv6RouterAdvertisement) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
if err := i.Options.SerializeTo(b, opts); err != nil {
return err
}
buf, err := b.PrependBytes(12)
if err != nil {
return err
}
buf[0] = byte(i.HopLimit)
buf[1] = byte(i.Flags)
binary.BigEndian.PutUint16(buf[2:], i.RouterLifetime)
binary.BigEndian.PutUint32(buf[4:], i.ReachableTime)
binary.BigEndian.PutUint32(buf[8:], i.RetransTimer)
return nil
}
// CanDecode returns the set of layer types that this DecodingLayer can decode.
func (i *ICMPv6RouterAdvertisement) CanDecode() gopacket.LayerClass {
return LayerTypeICMPv6RouterAdvertisement
}
// ManagedAddressConfig is true when addresses are available via DHCPv6. If
// set, the OtherConfig flag is redundant.
func (i *ICMPv6RouterAdvertisement) ManagedAddressConfig() bool {
return i.Flags&0x80 != 0
}
// OtherConfig is true when there is other configuration information available
// via DHCPv6. For example, DNS-related information.
func (i *ICMPv6RouterAdvertisement) OtherConfig() bool {
return i.Flags&0x40 != 0
}
// LayerType returns LayerTypeICMPv6NeighborSolicitation.
func (i *ICMPv6NeighborSolicitation) LayerType() gopacket.LayerType {
return LayerTypeICMPv6NeighborSolicitation
}
// NextLayerType returns the layer type contained by this DecodingLayer.
func (i *ICMPv6NeighborSolicitation) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypePayload
}
// DecodeFromBytes decodes the given bytes into this layer.
func (i *ICMPv6NeighborSolicitation) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
if len(data) < 20 {
df.SetTruncated()
return errors.New("ICMP layer less then 20 bytes for ICMPv6 neighbor solicitation")
}
i.TargetAddress = net.IP(data[4:20])
i.BaseLayer = BaseLayer{data, nil} // assume no payload
// truncate old options
i.Options = i.Options[:0]
return i.Options.DecodeFromBytes(data[20:], df)
}
// SerializeTo writes the serialized form of this layer into the
// SerializationBuffer, implementing gopacket.SerializableLayer.
// See the docs for gopacket.SerializableLayer for more info.
func (i *ICMPv6NeighborSolicitation) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
if err := i.Options.SerializeTo(b, opts); err != nil {
return err
}
buf, err := b.PrependBytes(20)
if err != nil {
return err
}
copy(buf, lotsOfZeros[:4])
copy(buf[4:], i.TargetAddress)
return nil
}
// CanDecode returns the set of layer types that this DecodingLayer can decode.
func (i *ICMPv6NeighborSolicitation) CanDecode() gopacket.LayerClass {
return LayerTypeICMPv6NeighborSolicitation
}
// LayerType returns LayerTypeICMPv6NeighborAdvertisement.
func (i *ICMPv6NeighborAdvertisement) LayerType() gopacket.LayerType {
return LayerTypeICMPv6NeighborAdvertisement
}
// NextLayerType returns the layer type contained by this DecodingLayer.
func (i *ICMPv6NeighborAdvertisement) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypePayload
}
// DecodeFromBytes decodes the given bytes into this layer.
func (i *ICMPv6NeighborAdvertisement) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
if len(data) < 20 {
df.SetTruncated()
return errors.New("ICMP layer less then 20 bytes for ICMPv6 neighbor advertisement")
}
i.Flags = uint8(data[0])
i.TargetAddress = net.IP(data[4:20])
i.BaseLayer = BaseLayer{data, nil} // assume no payload
// truncate old options
i.Options = i.Options[:0]
return i.Options.DecodeFromBytes(data[20:], df)
}
// SerializeTo writes the serialized form of this layer into the
// SerializationBuffer, implementing gopacket.SerializableLayer.
// See the docs for gopacket.SerializableLayer for more info.
func (i *ICMPv6NeighborAdvertisement) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
if err := i.Options.SerializeTo(b, opts); err != nil {
return err
}
buf, err := b.PrependBytes(20)
if err != nil {
return err
}
buf[0] = byte(i.Flags)
copy(buf[1:], lotsOfZeros[:3])
copy(buf[4:], i.TargetAddress)
return nil
}
// CanDecode returns the set of layer types that this DecodingLayer can decode.
func (i *ICMPv6NeighborAdvertisement) CanDecode() gopacket.LayerClass {
return LayerTypeICMPv6NeighborAdvertisement
}
// Router indicates whether the sender is a router or not.
func (i *ICMPv6NeighborAdvertisement) Router() bool {
return i.Flags&0x80 != 0
}
// Solicited indicates whether the advertisement was solicited or not.
func (i *ICMPv6NeighborAdvertisement) Solicited() bool {
return i.Flags&0x40 != 0
}
// Override indicates whether the advertisement should Override an existing
// cache entry.
func (i *ICMPv6NeighborAdvertisement) Override() bool {
return i.Flags&0x20 != 0
}
// LayerType returns LayerTypeICMPv6Redirect.
func (i *ICMPv6Redirect) LayerType() gopacket.LayerType {
return LayerTypeICMPv6Redirect
}
// NextLayerType returns the layer type contained by this DecodingLayer.
func (i *ICMPv6Redirect) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypePayload
}
// DecodeFromBytes decodes the given bytes into this layer.
func (i *ICMPv6Redirect) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
if len(data) < 36 {
df.SetTruncated()
return errors.New("ICMP layer less then 36 bytes for ICMPv6 redirect")
}
i.TargetAddress = net.IP(data[4:20])
i.DestinationAddress = net.IP(data[20:36])
i.BaseLayer = BaseLayer{data, nil} // assume no payload
// truncate old options
i.Options = i.Options[:0]
return i.Options.DecodeFromBytes(data[36:], df)
}
// SerializeTo writes the serialized form of this layer into the
// SerializationBuffer, implementing gopacket.SerializableLayer.
// See the docs for gopacket.SerializableLayer for more info.
func (i *ICMPv6Redirect) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
if err := i.Options.SerializeTo(b, opts); err != nil {
return err
}
buf, err := b.PrependBytes(36)
if err != nil {
return err
}
copy(buf, lotsOfZeros[:4])
copy(buf[4:], i.TargetAddress)
copy(buf[20:], i.DestinationAddress)
return nil
}
// CanDecode returns the set of layer types that this DecodingLayer can decode.
func (i *ICMPv6Redirect) CanDecode() gopacket.LayerClass {
return LayerTypeICMPv6Redirect
}
func (i ICMPv6Option) String() string {
hd := hex.EncodeToString(i.Data)
if len(hd) > 0 {
hd = " 0x" + hd
}
switch i.Type {
case ICMPv6OptSourceAddress, ICMPv6OptTargetAddress:
return fmt.Sprintf("ICMPv6Option(%s:%v)",
i.Type,
net.HardwareAddr(i.Data))
case ICMPv6OptPrefixInfo:
if len(i.Data) == 30 {
prefixLen := uint8(i.Data[0])
onLink := (i.Data[1]&0x80 != 0)
autonomous := (i.Data[1]&0x40 != 0)
validLifetime := time.Duration(binary.BigEndian.Uint32(i.Data[2:6])) * time.Second
preferredLifetime := time.Duration(binary.BigEndian.Uint32(i.Data[6:10])) * time.Second
prefix := net.IP(i.Data[14:])
return fmt.Sprintf("ICMPv6Option(%s:%v/%v:%t:%t:%v:%v)",
i.Type,
prefix, prefixLen,
onLink, autonomous,
validLifetime, preferredLifetime)
}
case ICMPv6OptRedirectedHeader:
// could invoke IP decoder on data... probably best not to
break
case ICMPv6OptMTU:
if len(i.Data) == 6 {
return fmt.Sprintf("ICMPv6Option(%s:%v)",
i.Type,
binary.BigEndian.Uint32(i.Data[2:]))
}
}
return fmt.Sprintf("ICMPv6Option(%s:%s)", i.Type, hd)
}
// DecodeFromBytes decodes the given bytes into this layer.
func (i *ICMPv6Options) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
for len(data) > 0 {
if len(data) < 2 {
df.SetTruncated()
return errors.New("ICMP layer less then 2 bytes for ICMPv6 message option")
}
// unit is 8 octets, convert to bytes
length := int(data[1]) * 8
if length == 0 {
df.SetTruncated()
return errors.New("ICMPv6 message option with length 0")
}
if len(data) < length {
df.SetTruncated()
return fmt.Errorf("ICMP layer only %v bytes for ICMPv6 message option with length %v", len(data), length)
}
o := ICMPv6Option{
Type: ICMPv6Opt(data[0]),
Data: data[2:length],
}
// chop off option we just consumed
data = data[length:]
*i = append(*i, o)
}
return nil
}
// SerializeTo writes the serialized form of this layer into the
// SerializationBuffer, implementing gopacket.SerializableLayer.
// See the docs for gopacket.SerializableLayer for more info.
func (i *ICMPv6Options) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
for _, opt := range []ICMPv6Option(*i) {
length := len(opt.Data) + 2
buf, err := b.PrependBytes(length)
if err != nil {
return err
}
buf[0] = byte(opt.Type)
buf[1] = byte(length / 8)
copy(buf[2:], opt.Data)
}
return nil
}
func decodeICMPv6Echo(data []byte, p gopacket.PacketBuilder) error {
i := &ICMPv6Echo{}
return decodingLayerDecoder(i, data, p)
}
func decodeICMPv6RouterSolicitation(data []byte, p gopacket.PacketBuilder) error {
i := &ICMPv6RouterSolicitation{}
return decodingLayerDecoder(i, data, p)
}
func decodeICMPv6RouterAdvertisement(data []byte, p gopacket.PacketBuilder) error {
i := &ICMPv6RouterAdvertisement{}
return decodingLayerDecoder(i, data, p)
}
func decodeICMPv6NeighborSolicitation(data []byte, p gopacket.PacketBuilder) error {
i := &ICMPv6NeighborSolicitation{}
return decodingLayerDecoder(i, data, p)
}
func decodeICMPv6NeighborAdvertisement(data []byte, p gopacket.PacketBuilder) error {
i := &ICMPv6NeighborAdvertisement{}
return decodingLayerDecoder(i, data, p)
}
func decodeICMPv6Redirect(data []byte, p gopacket.PacketBuilder) error {
i := &ICMPv6Redirect{}
return decodingLayerDecoder(i, data, p)
}