summaryrefslogtreecommitdiff
path: root/src/mongo/gotools/src/github.com/mongodb/mongo-tools/vendor/github.com/google/gopacket/layers/mldv2.go
blob: 248cf749efbc02bba289f88aa681ef68f2cb4dc5 (plain)
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
// Copyright 2018 GoPacket Authors. 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"
	"math"
	"net"
	"time"

	"github.com/google/gopacket"
)

const (
	// S Flag bit is 1
	mldv2STrue uint8 = 0x8

	// S Flag value mask
	//   mldv2STrue & mldv2SMask == mldv2STrue  // true
	//          0x1 & mldv2SMask == mldv2STrue  // true
	//          0x0 & mldv2SMask == mldv2STrue  // false
	mldv2SMask uint8 = 0x8

	// QRV value mask
	mldv2QRVMask uint8 = 0x7
)

// MLDv2MulticastListenerQueryMessage are sent by multicast routers to query the
// multicast listening state of neighboring interfaces.
// https://tools.ietf.org/html/rfc3810#section-5.1
//
// Some information, like Maximum Response Code and Multicast Address are in the
// previous layer LayerTypeMLDv1MulticastListenerQuery
type MLDv2MulticastListenerQueryMessage struct {
	BaseLayer
	// 5.1.3. Maximum Response Delay COde
	MaximumResponseCode uint16
	// 5.1.5. Multicast Address
	// Zero in general query
	// Specific IPv6 multicast address otherwise
	MulticastAddress net.IP
	// 5.1.7. S Flag (Suppress Router-Side Processing)
	SuppressRoutersideProcessing bool
	// 5.1.8. QRV (Querier's Robustness Variable)
	QueriersRobustnessVariable uint8
	// 5.1.9. QQIC (Querier's Query Interval Code)
	QueriersQueryIntervalCode uint8
	// 5.1.10. Number of Sources (N)
	NumberOfSources uint16
	// 5.1.11 Source Address [i]
	SourceAddresses []net.IP
}

// DecodeFromBytes decodes the given bytes into this layer.
func (m *MLDv2MulticastListenerQueryMessage) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
	if len(data) < 24 {
		df.SetTruncated()
		return errors.New("ICMP layer less than 24 bytes for Multicast Listener Query Message V2")
	}

	m.MaximumResponseCode = binary.BigEndian.Uint16(data[0:2])
	// ignore data[2:4] as per https://tools.ietf.org/html/rfc3810#section-5.1.4
	m.MulticastAddress = data[4:20]
	m.SuppressRoutersideProcessing = (data[20] & mldv2SMask) == mldv2STrue
	m.QueriersRobustnessVariable = data[20] & mldv2QRVMask
	m.QueriersQueryIntervalCode = data[21]

	m.NumberOfSources = binary.BigEndian.Uint16(data[22:24])

	var end int
	for i := uint16(0); i < m.NumberOfSources; i++ {
		begin := 24 + (int(i) * 16)
		end = begin + 16

		if end > len(data) {
			df.SetTruncated()
			return fmt.Errorf("ICMP layer less than %d bytes for Multicast Listener Query Message V2", end)
		}

		m.SourceAddresses = append(m.SourceAddresses, data[begin:end])
	}

	return nil
}

// NextLayerType returns the layer type contained by this DecodingLayer.
func (*MLDv2MulticastListenerQueryMessage) NextLayerType() gopacket.LayerType {
	return gopacket.LayerTypeZero
}

// SerializeTo writes the serialized form of this layer into the
// SerializationBuffer, implementing gopacket.SerializableLayer.
// See the docs for gopacket.SerializableLayer for more info.
func (m *MLDv2MulticastListenerQueryMessage) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
	if err := m.serializeSourceAddressesTo(b, opts); err != nil {
		return err
	}

	buf, err := b.PrependBytes(24)
	if err != nil {
		return err
	}

	binary.BigEndian.PutUint16(buf[0:2], m.MaximumResponseCode)
	copy(buf[2:4], []byte{0x00, 0x00}) // set reserved bytes to zero

	ma16 := m.MulticastAddress.To16()
	if ma16 == nil {
		return fmt.Errorf("invalid MulticastAddress '%s'", m.MulticastAddress)
	}
	copy(buf[4:20], ma16)

	byte20 := m.QueriersRobustnessVariable & mldv2QRVMask
	if m.SuppressRoutersideProcessing {
		byte20 |= mldv2STrue
	} else {
		byte20 &= ^mldv2STrue // the complement of mldv2STrue
	}
	byte20 &= 0x0F // set reserved bits to zero
	buf[20] = byte20

	binary.BigEndian.PutUint16(buf[22:24], m.NumberOfSources)
	buf[21] = m.QueriersQueryIntervalCode

	return nil
}

// writes each source address to the buffer preserving the order
func (m *MLDv2MulticastListenerQueryMessage) serializeSourceAddressesTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
	numberOfSourceAddresses := len(m.SourceAddresses)
	if numberOfSourceAddresses > math.MaxUint16 {
		return fmt.Errorf(
			"there are more than %d source addresses, but 65535 is the maximum number of supported addresses",
			numberOfSourceAddresses)
	}

	if opts.FixLengths {
		m.NumberOfSources = uint16(numberOfSourceAddresses)
	}

	lastSAIdx := numberOfSourceAddresses - 1
	for k := range m.SourceAddresses {
		i := lastSAIdx - k // reverse order

		buf, err := b.PrependBytes(16)
		if err != nil {
			return err
		}

		sa16 := m.SourceAddresses[i].To16()
		if sa16 == nil {
			return fmt.Errorf("invalid source address [%d] '%s'", i, m.SourceAddresses[i])
		}
		copy(buf[0:16], sa16)
	}

	return nil
}

// String sums this layer up nicely formatted
func (m *MLDv2MulticastListenerQueryMessage) String() string {
	return fmt.Sprintf(
		"Maximum Response Code: %#x (%dms), Multicast Address: %s, Suppress Routerside Processing: %t, QRV: %#x, QQIC: %#x (%ds), Number of Source Address: %d (actual: %d), Source Addresses: %s",
		m.MaximumResponseCode,
		m.MaximumResponseDelay(),
		m.MulticastAddress,
		m.SuppressRoutersideProcessing,
		m.QueriersRobustnessVariable,
		m.QueriersQueryIntervalCode,
		m.QQI()/time.Second,
		m.NumberOfSources,
		len(m.SourceAddresses),
		m.SourceAddresses)
}

// LayerType returns LayerTypeMLDv2MulticastListenerQuery.
func (*MLDv2MulticastListenerQueryMessage) LayerType() gopacket.LayerType {
	return LayerTypeMLDv2MulticastListenerQuery
}

// CanDecode returns the set of layer types that this DecodingLayer can decode.
func (*MLDv2MulticastListenerQueryMessage) CanDecode() gopacket.LayerClass {
	return LayerTypeMLDv2MulticastListenerQuery
}

// QQI calculates the Querier's Query Interval based on the QQIC
// according to https://tools.ietf.org/html/rfc3810#section-5.1.9
func (m *MLDv2MulticastListenerQueryMessage) QQI() time.Duration {
	data := m.QueriersQueryIntervalCode
	if data < 128 {
		return time.Second * time.Duration(data)
	}

	exp := uint16(data) & 0x70 >> 4
	mant := uint16(data) & 0x0F
	return time.Second * time.Duration(mant|0x1000<<(exp+3))
}

// SetQQI calculates and updates the Querier's Query Interval Code (QQIC)
// according to https://tools.ietf.org/html/rfc3810#section-5.1.9
func (m *MLDv2MulticastListenerQueryMessage) SetQQI(d time.Duration) error {
	if d < 0 {
		m.QueriersQueryIntervalCode = 0
		return errors.New("QQI duration is negative")
	}

	if d == 0 {
		m.QueriersQueryIntervalCode = 0
		return nil
	}

	dms := d / time.Second
	if dms < 128 {
		m.QueriersQueryIntervalCode = uint8(dms)
	}

	if dms > 31744 { // mant=0xF, exp=0x7
		m.QueriersQueryIntervalCode = 0xFF
		return fmt.Errorf("QQI duration %ds is, maximum allowed is 31744s", dms)
	}

	value := uint16(dms) // ok, because 31744 < math.MaxUint16
	exp := uint8(7)
	for mask := uint16(0x4000); exp > 0; exp-- {
		if mask&value != 0 {
			break
		}

		mask >>= 1
	}

	mant := uint8(0x000F & (value >> (exp + 3)))
	sig := uint8(0x10)
	m.QueriersQueryIntervalCode = sig | exp<<4 | mant

	return nil
}

// MaximumResponseDelay returns the Maximum Response Delay based on the
// Maximum Response Code according to
// https://tools.ietf.org/html/rfc3810#section-5.1.3
func (m *MLDv2MulticastListenerQueryMessage) MaximumResponseDelay() time.Duration {
	if m.MaximumResponseCode < 0x8000 {
		return time.Duration(m.MaximumResponseCode)
	}

	exp := m.MaximumResponseCode & 0x7000 >> 12
	mant := m.MaximumResponseCode & 0x0FFF

	return time.Millisecond * time.Duration(mant|0x1000<<(exp+3))
}

// SetMLDv2MaximumResponseDelay updates the Maximum Response Code according to
// https://tools.ietf.org/html/rfc3810#section-5.1.3
func (m *MLDv2MulticastListenerQueryMessage) SetMLDv2MaximumResponseDelay(d time.Duration) error {
	if d == 0 {
		m.MaximumResponseCode = 0
		return nil
	}

	if d < 0 {
		return errors.New("maximum response delay must not be negative")
	}

	dms := d / time.Millisecond

	if dms < 32768 {
		m.MaximumResponseCode = uint16(dms)
	}

	if dms > 4193280 { // mant=0xFFF, exp=0x7
		return fmt.Errorf("maximum response delay %dms is bigger the than maximum of 4193280ms", dms)
	}

	value := uint32(dms) // ok, because 4193280 < math.MaxUint32
	exp := uint8(7)
	for mask := uint32(0x40000000); exp > 0; exp-- {
		if mask&value != 0 {
			break
		}

		mask >>= 1
	}

	mant := uint16(0x00000FFF & (value >> (exp + 3)))
	sig := uint16(0x1000)
	m.MaximumResponseCode = sig | uint16(exp)<<12 | mant
	return nil
}

// MLDv2MulticastListenerReportMessage is sent by an IP node to report the
// current multicast listening state, or changes therein.
// https://tools.ietf.org/html/rfc3810#section-5.2
type MLDv2MulticastListenerReportMessage struct {
	BaseLayer
	// 5.2.3. Nr of Mcast Address Records
	NumberOfMulticastAddressRecords uint16
	// 5.2.4. Multicast Address Record [i]
	MulticastAddressRecords []MLDv2MulticastAddressRecord
}

// DecodeFromBytes decodes the given bytes into this layer.
func (m *MLDv2MulticastListenerReportMessage) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
	if len(data) < 4 {
		df.SetTruncated()
		return errors.New("ICMP layer less than 4 bytes for Multicast Listener Report Message V2")
	}

	// ignore data[0:2] as per RFC
	// https://tools.ietf.org/html/rfc3810#section-5.2.1
	m.NumberOfMulticastAddressRecords = binary.BigEndian.Uint16(data[2:4])

	begin := 4
	for i := uint16(0); i < m.NumberOfMulticastAddressRecords; i++ {
		mar := MLDv2MulticastAddressRecord{}
		read, err := mar.decode(data[begin:], df)
		if err != nil {
			return err
		}

		m.MulticastAddressRecords = append(m.MulticastAddressRecords, mar)

		begin += read
	}

	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 (m *MLDv2MulticastListenerReportMessage) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
	lastItemIdx := len(m.MulticastAddressRecords) - 1
	for k := range m.MulticastAddressRecords {
		i := lastItemIdx - k // reverse order

		err := m.MulticastAddressRecords[i].serializeTo(b, opts)
		if err != nil {
			return err
		}
	}

	if opts.FixLengths {
		numberOfMAR := len(m.MulticastAddressRecords)
		if numberOfMAR > math.MaxUint16 {
			return fmt.Errorf(
				"%d multicast address records added, but the maximum is 65535",
				numberOfMAR)
		}

		m.NumberOfMulticastAddressRecords = uint16(numberOfMAR)
	}

	buf, err := b.PrependBytes(4)
	if err != nil {
		return err
	}

	copy(buf[0:2], []byte{0x0, 0x0})
	binary.BigEndian.PutUint16(buf[2:4], m.NumberOfMulticastAddressRecords)
	return nil
}

// Sums this layer up nicely formatted
func (m *MLDv2MulticastListenerReportMessage) String() string {
	return fmt.Sprintf(
		"Number of Mcast Addr Records: %d (actual %d), Multicast Address Records: %+v",
		m.NumberOfMulticastAddressRecords,
		len(m.MulticastAddressRecords),
		m.MulticastAddressRecords)
}

// LayerType returns LayerTypeMLDv2MulticastListenerQuery.
func (*MLDv2MulticastListenerReportMessage) LayerType() gopacket.LayerType {
	return LayerTypeMLDv2MulticastListenerReport
}

// CanDecode returns the set of layer types that this DecodingLayer can decode.
func (*MLDv2MulticastListenerReportMessage) CanDecode() gopacket.LayerClass {
	return LayerTypeMLDv2MulticastListenerReport
}

// NextLayerType returns the layer type contained by this DecodingLayer.
func (*MLDv2MulticastListenerReportMessage) NextLayerType() gopacket.LayerType {
	return gopacket.LayerTypePayload
}

// MLDv2MulticastAddressRecordType holds the type of a
// Multicast Address Record, according to
// https://tools.ietf.org/html/rfc3810#section-5.2.5 and
// https://tools.ietf.org/html/rfc3810#section-5.2.12
type MLDv2MulticastAddressRecordType uint8

const (
	// MLDv2MulticastAddressRecordTypeModeIsIncluded stands for
	// MODE_IS_INCLUDE - indicates that the interface has a filter
	// mode of INCLUDE for the specified multicast address.
	MLDv2MulticastAddressRecordTypeModeIsIncluded MLDv2MulticastAddressRecordType = 1
	// MLDv2MulticastAddressRecordTypeModeIsExcluded stands for
	// MODE_IS_EXCLUDE - indicates that the interface has a filter
	// mode of EXCLUDE for the specified multicast address.
	MLDv2MulticastAddressRecordTypeModeIsExcluded MLDv2MulticastAddressRecordType = 2
	// MLDv2MulticastAddressRecordTypeChangeToIncludeMode stands for
	// CHANGE_TO_INCLUDE_MODE - indicates that the interface has
	// changed to INCLUDE filter mode for the specified multicast
	// address.
	MLDv2MulticastAddressRecordTypeChangeToIncludeMode MLDv2MulticastAddressRecordType = 3
	// MLDv2MulticastAddressRecordTypeChangeToExcludeMode stands for
	// CHANGE_TO_EXCLUDE_MODE - indicates that the interface has
	// changed to EXCLUDE filter mode for the specified multicast
	// address
	MLDv2MulticastAddressRecordTypeChangeToExcludeMode MLDv2MulticastAddressRecordType = 4
	// MLDv2MulticastAddressRecordTypeAllowNewSources stands for
	// ALLOW_NEW_SOURCES - indicates that the Source Address [i]
	// fields in this Multicast Address Record contain a list of
	// the additional sources that the node wishes to listen to,
	// for packets sent to the specified multicast address.
	MLDv2MulticastAddressRecordTypeAllowNewSources MLDv2MulticastAddressRecordType = 5
	// MLDv2MulticastAddressRecordTypeBlockOldSources stands for
	// BLOCK_OLD_SOURCES - indicates that the Source Address [i]
	// fields in this Multicast Address Record contain a list of
	// the sources that the node no longer wishes to listen to,
	// for packets sent to the specified multicast address.
	MLDv2MulticastAddressRecordTypeBlockOldSources MLDv2MulticastAddressRecordType = 6
)

// Human readable record types
// Naming follows https://tools.ietf.org/html/rfc3810#section-5.2.12
func (m MLDv2MulticastAddressRecordType) String() string {
	switch m {
	case MLDv2MulticastAddressRecordTypeModeIsIncluded:
		return "MODE_IS_INCLUDE"
	case MLDv2MulticastAddressRecordTypeModeIsExcluded:
		return "MODE_IS_EXCLUDE"
	case MLDv2MulticastAddressRecordTypeChangeToIncludeMode:
		return "CHANGE_TO_INCLUDE_MODE"
	case MLDv2MulticastAddressRecordTypeChangeToExcludeMode:
		return "CHANGE_TO_EXCLUDE_MODE"
	case MLDv2MulticastAddressRecordTypeAllowNewSources:
		return "ALLOW_NEW_SOURCES"
	case MLDv2MulticastAddressRecordTypeBlockOldSources:
		return "BLOCK_OLD_SOURCES"
	default:
		return fmt.Sprintf("UNKNOWN(%d)", m)
	}
}

// MLDv2MulticastAddressRecord contains information on the sender listening to a
// single multicast address on the interface the report is sent.
// https://tools.ietf.org/html/rfc3810#section-5.2.4
type MLDv2MulticastAddressRecord struct {
	// 5.2.5. Record Type
	RecordType MLDv2MulticastAddressRecordType
	// 5.2.6. Auxiliary Data Length (number of 32-bit words)
	AuxDataLen uint8
	// 5.2.7. Number Of Sources (N)
	N uint16
	// 5.2.8. Multicast Address
	MulticastAddress net.IP
	// 5.2.9 Source Address [i]
	SourceAddresses []net.IP
	// 5.2.10 Auxiliary Data
	AuxiliaryData []byte
}

// decodes a multicast address record from bytes
func (m *MLDv2MulticastAddressRecord) decode(data []byte, df gopacket.DecodeFeedback) (int, error) {
	if len(data) < 4 {
		df.SetTruncated()
		return 0, errors.New(
			"Multicast Listener Report Message V2 layer less than 4 bytes for Multicast Address Record")
	}

	m.RecordType = MLDv2MulticastAddressRecordType(data[0])
	m.AuxDataLen = data[1]
	m.N = binary.BigEndian.Uint16(data[2:4])
	m.MulticastAddress = data[4:20]

	for i := uint16(0); i < m.N; i++ {
		begin := 20 + (int(i) * 16)
		end := begin + 16

		if len(data) < end {
			df.SetTruncated()
			return begin, fmt.Errorf(
				"Multicast Listener Report Message V2 layer less than %d bytes for Multicast Address Record", end)
		}

		m.SourceAddresses = append(m.SourceAddresses, data[begin:end])
	}

	expectedLengthWithouAuxData := 20 + (int(m.N) * 16)
	expectedTotalLength := (int(m.AuxDataLen) * 4) + expectedLengthWithouAuxData // *4 because AuxDataLen are 32bit words
	if len(data) < expectedTotalLength {
		return expectedLengthWithouAuxData, fmt.Errorf(
			"Multicast Listener Report Message V2 layer less than %d bytes for Multicast Address Record",
			expectedLengthWithouAuxData)
	}

	m.AuxiliaryData = data[expectedLengthWithouAuxData:expectedTotalLength]

	return expectedTotalLength, nil
}

// String sums this layer up nicely formatted
func (m *MLDv2MulticastAddressRecord) String() string {
	return fmt.Sprintf(
		"RecordType: %d (%s), AuxDataLen: %d [32-bit words], N: %d, Multicast Address: %s, SourceAddresses: %s, Auxiliary Data: %#x",
		m.RecordType,
		m.RecordType.String(),
		m.AuxDataLen,
		m.N,
		m.MulticastAddress.To16(),
		m.SourceAddresses,
		m.AuxiliaryData)
}

// serializes a multicast address record
func (m *MLDv2MulticastAddressRecord) serializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
	if err := m.serializeAuxiliaryDataTo(b, opts); err != nil {
		return err
	}

	if err := m.serializeSourceAddressesTo(b, opts); err != nil {
		return err
	}

	buf, err := b.PrependBytes(20)
	if err != nil {
		return err
	}

	buf[0] = uint8(m.RecordType)
	buf[1] = m.AuxDataLen
	binary.BigEndian.PutUint16(buf[2:4], m.N)

	ma16 := m.MulticastAddress.To16()
	if ma16 == nil {
		return fmt.Errorf("invalid multicast address '%s'", m.MulticastAddress)
	}
	copy(buf[4:20], ma16)

	return nil
}

// serializes the auxiliary data of a multicast address record
func (m *MLDv2MulticastAddressRecord) serializeAuxiliaryDataTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
	if remainder := len(m.AuxiliaryData) % 4; remainder != 0 {
		zeroWord := []byte{0x0, 0x0, 0x0, 0x0}
		m.AuxiliaryData = append(m.AuxiliaryData, zeroWord[:remainder]...)
	}

	if opts.FixLengths {
		auxDataLen := len(m.AuxiliaryData) / 4

		if auxDataLen > math.MaxUint8 {
			return fmt.Errorf("auxilary data is %d 32-bit words, but the maximum is 255 32-bit words", auxDataLen)
		}

		m.AuxDataLen = uint8(auxDataLen)
	}

	buf, err := b.PrependBytes(len(m.AuxiliaryData))
	if err != nil {
		return err
	}

	copy(buf, m.AuxiliaryData)
	return nil
}

// serializes the source addresses of a multicast address record preserving the order
func (m *MLDv2MulticastAddressRecord) serializeSourceAddressesTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
	if opts.FixLengths {
		numberOfSourceAddresses := len(m.SourceAddresses)

		if numberOfSourceAddresses > math.MaxUint16 {
			return fmt.Errorf(
				"%d source addresses added, but the maximum is 65535",
				numberOfSourceAddresses)
		}

		m.N = uint16(numberOfSourceAddresses)
	}

	lastItemIdx := len(m.SourceAddresses) - 1
	for k := range m.SourceAddresses {
		i := lastItemIdx - k // reverse order

		buf, err := b.PrependBytes(16)
		if err != nil {
			return err
		}

		sa16 := m.SourceAddresses[i].To16()
		if sa16 == nil {
			return fmt.Errorf("invalid source address [%d] '%s'", i, m.SourceAddresses[i])
		}
		copy(buf, sa16)
	}

	return nil
}

func decodeMLDv2MulticastListenerReport(data []byte, p gopacket.PacketBuilder) error {
	m := &MLDv2MulticastListenerReportMessage{}
	return decodingLayerDecoder(m, data, p)
}

func decodeMLDv2MulticastListenerQuery(data []byte, p gopacket.PacketBuilder) error {
	m := &MLDv2MulticastListenerQueryMessage{}
	return decodingLayerDecoder(m, data, p)
}