mldv2.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. // Copyright 2018 GoPacket Authors. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the LICENSE file in the root of the source
  5. // tree.
  6. package layers
  7. import (
  8. "encoding/binary"
  9. "errors"
  10. "fmt"
  11. "math"
  12. "net"
  13. "time"
  14. "github.com/google/gopacket"
  15. )
  16. const (
  17. // S Flag bit is 1
  18. mldv2STrue uint8 = 0x8
  19. // S Flag value mask
  20. // mldv2STrue & mldv2SMask == mldv2STrue // true
  21. // 0x1 & mldv2SMask == mldv2STrue // true
  22. // 0x0 & mldv2SMask == mldv2STrue // false
  23. mldv2SMask uint8 = 0x8
  24. // QRV value mask
  25. mldv2QRVMask uint8 = 0x7
  26. )
  27. // MLDv2MulticastListenerQueryMessage are sent by multicast routers to query the
  28. // multicast listening state of neighboring interfaces.
  29. // https://tools.ietf.org/html/rfc3810#section-5.1
  30. //
  31. // Some information, like Maximum Response Code and Multicast Address are in the
  32. // previous layer LayerTypeMLDv1MulticastListenerQuery
  33. type MLDv2MulticastListenerQueryMessage struct {
  34. BaseLayer
  35. // 5.1.3. Maximum Response Delay COde
  36. MaximumResponseCode uint16
  37. // 5.1.5. Multicast Address
  38. // Zero in general query
  39. // Specific IPv6 multicast address otherwise
  40. MulticastAddress net.IP
  41. // 5.1.7. S Flag (Suppress Router-Side Processing)
  42. SuppressRoutersideProcessing bool
  43. // 5.1.8. QRV (Querier's Robustness Variable)
  44. QueriersRobustnessVariable uint8
  45. // 5.1.9. QQIC (Querier's Query Interval Code)
  46. QueriersQueryIntervalCode uint8
  47. // 5.1.10. Number of Sources (N)
  48. NumberOfSources uint16
  49. // 5.1.11 Source Address [i]
  50. SourceAddresses []net.IP
  51. }
  52. // DecodeFromBytes decodes the given bytes into this layer.
  53. func (m *MLDv2MulticastListenerQueryMessage) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
  54. if len(data) < 24 {
  55. df.SetTruncated()
  56. return errors.New("ICMP layer less than 24 bytes for Multicast Listener Query Message V2")
  57. }
  58. m.MaximumResponseCode = binary.BigEndian.Uint16(data[0:2])
  59. // ignore data[2:4] as per https://tools.ietf.org/html/rfc3810#section-5.1.4
  60. m.MulticastAddress = data[4:20]
  61. m.SuppressRoutersideProcessing = (data[20] & mldv2SMask) == mldv2STrue
  62. m.QueriersRobustnessVariable = data[20] & mldv2QRVMask
  63. m.QueriersQueryIntervalCode = data[21]
  64. m.NumberOfSources = binary.BigEndian.Uint16(data[22:24])
  65. var end int
  66. for i := uint16(0); i < m.NumberOfSources; i++ {
  67. begin := 24 + (int(i) * 16)
  68. end = begin + 16
  69. if end > len(data) {
  70. df.SetTruncated()
  71. return fmt.Errorf("ICMP layer less than %d bytes for Multicast Listener Query Message V2", end)
  72. }
  73. m.SourceAddresses = append(m.SourceAddresses, data[begin:end])
  74. }
  75. return nil
  76. }
  77. // NextLayerType returns the layer type contained by this DecodingLayer.
  78. func (*MLDv2MulticastListenerQueryMessage) NextLayerType() gopacket.LayerType {
  79. return gopacket.LayerTypeZero
  80. }
  81. // SerializeTo writes the serialized form of this layer into the
  82. // SerializationBuffer, implementing gopacket.SerializableLayer.
  83. // See the docs for gopacket.SerializableLayer for more info.
  84. func (m *MLDv2MulticastListenerQueryMessage) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
  85. if err := m.serializeSourceAddressesTo(b, opts); err != nil {
  86. return err
  87. }
  88. buf, err := b.PrependBytes(24)
  89. if err != nil {
  90. return err
  91. }
  92. binary.BigEndian.PutUint16(buf[0:2], m.MaximumResponseCode)
  93. copy(buf[2:4], []byte{0x00, 0x00}) // set reserved bytes to zero
  94. ma16 := m.MulticastAddress.To16()
  95. if ma16 == nil {
  96. return fmt.Errorf("invalid MulticastAddress '%s'", m.MulticastAddress)
  97. }
  98. copy(buf[4:20], ma16)
  99. byte20 := m.QueriersRobustnessVariable & mldv2QRVMask
  100. if m.SuppressRoutersideProcessing {
  101. byte20 |= mldv2STrue
  102. } else {
  103. byte20 &= ^mldv2STrue // the complement of mldv2STrue
  104. }
  105. byte20 &= 0x0F // set reserved bits to zero
  106. buf[20] = byte20
  107. binary.BigEndian.PutUint16(buf[22:24], m.NumberOfSources)
  108. buf[21] = m.QueriersQueryIntervalCode
  109. return nil
  110. }
  111. // writes each source address to the buffer preserving the order
  112. func (m *MLDv2MulticastListenerQueryMessage) serializeSourceAddressesTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
  113. numberOfSourceAddresses := len(m.SourceAddresses)
  114. if numberOfSourceAddresses > math.MaxUint16 {
  115. return fmt.Errorf(
  116. "there are more than %d source addresses, but 65535 is the maximum number of supported addresses",
  117. numberOfSourceAddresses)
  118. }
  119. if opts.FixLengths {
  120. m.NumberOfSources = uint16(numberOfSourceAddresses)
  121. }
  122. lastSAIdx := numberOfSourceAddresses - 1
  123. for k := range m.SourceAddresses {
  124. i := lastSAIdx - k // reverse order
  125. buf, err := b.PrependBytes(16)
  126. if err != nil {
  127. return err
  128. }
  129. sa16 := m.SourceAddresses[i].To16()
  130. if sa16 == nil {
  131. return fmt.Errorf("invalid source address [%d] '%s'", i, m.SourceAddresses[i])
  132. }
  133. copy(buf[0:16], sa16)
  134. }
  135. return nil
  136. }
  137. // String sums this layer up nicely formatted
  138. func (m *MLDv2MulticastListenerQueryMessage) String() string {
  139. return fmt.Sprintf(
  140. "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",
  141. m.MaximumResponseCode,
  142. m.MaximumResponseDelay(),
  143. m.MulticastAddress,
  144. m.SuppressRoutersideProcessing,
  145. m.QueriersRobustnessVariable,
  146. m.QueriersQueryIntervalCode,
  147. m.QQI()/time.Second,
  148. m.NumberOfSources,
  149. len(m.SourceAddresses),
  150. m.SourceAddresses)
  151. }
  152. // LayerType returns LayerTypeMLDv2MulticastListenerQuery.
  153. func (*MLDv2MulticastListenerQueryMessage) LayerType() gopacket.LayerType {
  154. return LayerTypeMLDv2MulticastListenerQuery
  155. }
  156. // CanDecode returns the set of layer types that this DecodingLayer can decode.
  157. func (*MLDv2MulticastListenerQueryMessage) CanDecode() gopacket.LayerClass {
  158. return LayerTypeMLDv2MulticastListenerQuery
  159. }
  160. // QQI calculates the Querier's Query Interval based on the QQIC
  161. // according to https://tools.ietf.org/html/rfc3810#section-5.1.9
  162. func (m *MLDv2MulticastListenerQueryMessage) QQI() time.Duration {
  163. data := m.QueriersQueryIntervalCode
  164. if data < 128 {
  165. return time.Second * time.Duration(data)
  166. }
  167. exp := uint16(data) & 0x70 >> 4
  168. mant := uint16(data) & 0x0F
  169. return time.Second * time.Duration(mant|0x1000<<(exp+3))
  170. }
  171. // SetQQI calculates and updates the Querier's Query Interval Code (QQIC)
  172. // according to https://tools.ietf.org/html/rfc3810#section-5.1.9
  173. func (m *MLDv2MulticastListenerQueryMessage) SetQQI(d time.Duration) error {
  174. if d < 0 {
  175. m.QueriersQueryIntervalCode = 0
  176. return errors.New("QQI duration is negative")
  177. }
  178. if d == 0 {
  179. m.QueriersQueryIntervalCode = 0
  180. return nil
  181. }
  182. dms := d / time.Second
  183. if dms < 128 {
  184. m.QueriersQueryIntervalCode = uint8(dms)
  185. }
  186. if dms > 31744 { // mant=0xF, exp=0x7
  187. m.QueriersQueryIntervalCode = 0xFF
  188. return fmt.Errorf("QQI duration %ds is, maximum allowed is 31744s", dms)
  189. }
  190. value := uint16(dms) // ok, because 31744 < math.MaxUint16
  191. exp := uint8(7)
  192. for mask := uint16(0x4000); exp > 0; exp-- {
  193. if mask&value != 0 {
  194. break
  195. }
  196. mask >>= 1
  197. }
  198. mant := uint8(0x000F & (value >> (exp + 3)))
  199. sig := uint8(0x10)
  200. m.QueriersQueryIntervalCode = sig | exp<<4 | mant
  201. return nil
  202. }
  203. // MaximumResponseDelay returns the Maximum Response Delay based on the
  204. // Maximum Response Code according to
  205. // https://tools.ietf.org/html/rfc3810#section-5.1.3
  206. func (m *MLDv2MulticastListenerQueryMessage) MaximumResponseDelay() time.Duration {
  207. if m.MaximumResponseCode < 0x8000 {
  208. return time.Duration(m.MaximumResponseCode)
  209. }
  210. exp := m.MaximumResponseCode & 0x7000 >> 12
  211. mant := m.MaximumResponseCode & 0x0FFF
  212. return time.Millisecond * time.Duration(mant|0x1000<<(exp+3))
  213. }
  214. // SetMLDv2MaximumResponseDelay updates the Maximum Response Code according to
  215. // https://tools.ietf.org/html/rfc3810#section-5.1.3
  216. func (m *MLDv2MulticastListenerQueryMessage) SetMLDv2MaximumResponseDelay(d time.Duration) error {
  217. if d == 0 {
  218. m.MaximumResponseCode = 0
  219. return nil
  220. }
  221. if d < 0 {
  222. return errors.New("maximum response delay must not be negative")
  223. }
  224. dms := d / time.Millisecond
  225. if dms < 32768 {
  226. m.MaximumResponseCode = uint16(dms)
  227. }
  228. if dms > 4193280 { // mant=0xFFF, exp=0x7
  229. return fmt.Errorf("maximum response delay %dms is bigger the than maximum of 4193280ms", dms)
  230. }
  231. value := uint32(dms) // ok, because 4193280 < math.MaxUint32
  232. exp := uint8(7)
  233. for mask := uint32(0x40000000); exp > 0; exp-- {
  234. if mask&value != 0 {
  235. break
  236. }
  237. mask >>= 1
  238. }
  239. mant := uint16(0x00000FFF & (value >> (exp + 3)))
  240. sig := uint16(0x1000)
  241. m.MaximumResponseCode = sig | uint16(exp)<<12 | mant
  242. return nil
  243. }
  244. // MLDv2MulticastListenerReportMessage is sent by an IP node to report the
  245. // current multicast listening state, or changes therein.
  246. // https://tools.ietf.org/html/rfc3810#section-5.2
  247. type MLDv2MulticastListenerReportMessage struct {
  248. BaseLayer
  249. // 5.2.3. Nr of Mcast Address Records
  250. NumberOfMulticastAddressRecords uint16
  251. // 5.2.4. Multicast Address Record [i]
  252. MulticastAddressRecords []MLDv2MulticastAddressRecord
  253. }
  254. // DecodeFromBytes decodes the given bytes into this layer.
  255. func (m *MLDv2MulticastListenerReportMessage) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
  256. if len(data) < 4 {
  257. df.SetTruncated()
  258. return errors.New("ICMP layer less than 4 bytes for Multicast Listener Report Message V2")
  259. }
  260. // ignore data[0:2] as per RFC
  261. // https://tools.ietf.org/html/rfc3810#section-5.2.1
  262. m.NumberOfMulticastAddressRecords = binary.BigEndian.Uint16(data[2:4])
  263. begin := 4
  264. for i := uint16(0); i < m.NumberOfMulticastAddressRecords; i++ {
  265. mar := MLDv2MulticastAddressRecord{}
  266. read, err := mar.decode(data[begin:], df)
  267. if err != nil {
  268. return err
  269. }
  270. m.MulticastAddressRecords = append(m.MulticastAddressRecords, mar)
  271. begin += read
  272. }
  273. return nil
  274. }
  275. // SerializeTo writes the serialized form of this layer into the
  276. // SerializationBuffer, implementing gopacket.SerializableLayer.
  277. // See the docs for gopacket.SerializableLayer for more info.
  278. func (m *MLDv2MulticastListenerReportMessage) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
  279. lastItemIdx := len(m.MulticastAddressRecords) - 1
  280. for k := range m.MulticastAddressRecords {
  281. i := lastItemIdx - k // reverse order
  282. err := m.MulticastAddressRecords[i].serializeTo(b, opts)
  283. if err != nil {
  284. return err
  285. }
  286. }
  287. if opts.FixLengths {
  288. numberOfMAR := len(m.MulticastAddressRecords)
  289. if numberOfMAR > math.MaxUint16 {
  290. return fmt.Errorf(
  291. "%d multicast address records added, but the maximum is 65535",
  292. numberOfMAR)
  293. }
  294. m.NumberOfMulticastAddressRecords = uint16(numberOfMAR)
  295. }
  296. buf, err := b.PrependBytes(4)
  297. if err != nil {
  298. return err
  299. }
  300. copy(buf[0:2], []byte{0x0, 0x0})
  301. binary.BigEndian.PutUint16(buf[2:4], m.NumberOfMulticastAddressRecords)
  302. return nil
  303. }
  304. // Sums this layer up nicely formatted
  305. func (m *MLDv2MulticastListenerReportMessage) String() string {
  306. return fmt.Sprintf(
  307. "Number of Mcast Addr Records: %d (actual %d), Multicast Address Records: %+v",
  308. m.NumberOfMulticastAddressRecords,
  309. len(m.MulticastAddressRecords),
  310. m.MulticastAddressRecords)
  311. }
  312. // LayerType returns LayerTypeMLDv2MulticastListenerQuery.
  313. func (*MLDv2MulticastListenerReportMessage) LayerType() gopacket.LayerType {
  314. return LayerTypeMLDv2MulticastListenerReport
  315. }
  316. // CanDecode returns the set of layer types that this DecodingLayer can decode.
  317. func (*MLDv2MulticastListenerReportMessage) CanDecode() gopacket.LayerClass {
  318. return LayerTypeMLDv2MulticastListenerReport
  319. }
  320. // NextLayerType returns the layer type contained by this DecodingLayer.
  321. func (*MLDv2MulticastListenerReportMessage) NextLayerType() gopacket.LayerType {
  322. return gopacket.LayerTypePayload
  323. }
  324. // MLDv2MulticastAddressRecordType holds the type of a
  325. // Multicast Address Record, according to
  326. // https://tools.ietf.org/html/rfc3810#section-5.2.5 and
  327. // https://tools.ietf.org/html/rfc3810#section-5.2.12
  328. type MLDv2MulticastAddressRecordType uint8
  329. const (
  330. // MLDv2MulticastAddressRecordTypeModeIsIncluded stands for
  331. // MODE_IS_INCLUDE - indicates that the interface has a filter
  332. // mode of INCLUDE for the specified multicast address.
  333. MLDv2MulticastAddressRecordTypeModeIsIncluded MLDv2MulticastAddressRecordType = 1
  334. // MLDv2MulticastAddressRecordTypeModeIsExcluded stands for
  335. // MODE_IS_EXCLUDE - indicates that the interface has a filter
  336. // mode of EXCLUDE for the specified multicast address.
  337. MLDv2MulticastAddressRecordTypeModeIsExcluded MLDv2MulticastAddressRecordType = 2
  338. // MLDv2MulticastAddressRecordTypeChangeToIncludeMode stands for
  339. // CHANGE_TO_INCLUDE_MODE - indicates that the interface has
  340. // changed to INCLUDE filter mode for the specified multicast
  341. // address.
  342. MLDv2MulticastAddressRecordTypeChangeToIncludeMode MLDv2MulticastAddressRecordType = 3
  343. // MLDv2MulticastAddressRecordTypeChangeToExcludeMode stands for
  344. // CHANGE_TO_EXCLUDE_MODE - indicates that the interface has
  345. // changed to EXCLUDE filter mode for the specified multicast
  346. // address
  347. MLDv2MulticastAddressRecordTypeChangeToExcludeMode MLDv2MulticastAddressRecordType = 4
  348. // MLDv2MulticastAddressRecordTypeAllowNewSources stands for
  349. // ALLOW_NEW_SOURCES - indicates that the Source Address [i]
  350. // fields in this Multicast Address Record contain a list of
  351. // the additional sources that the node wishes to listen to,
  352. // for packets sent to the specified multicast address.
  353. MLDv2MulticastAddressRecordTypeAllowNewSources MLDv2MulticastAddressRecordType = 5
  354. // MLDv2MulticastAddressRecordTypeBlockOldSources stands for
  355. // BLOCK_OLD_SOURCES - indicates that the Source Address [i]
  356. // fields in this Multicast Address Record contain a list of
  357. // the sources that the node no longer wishes to listen to,
  358. // for packets sent to the specified multicast address.
  359. MLDv2MulticastAddressRecordTypeBlockOldSources MLDv2MulticastAddressRecordType = 6
  360. )
  361. // Human readable record types
  362. // Naming follows https://tools.ietf.org/html/rfc3810#section-5.2.12
  363. func (m MLDv2MulticastAddressRecordType) String() string {
  364. switch m {
  365. case MLDv2MulticastAddressRecordTypeModeIsIncluded:
  366. return "MODE_IS_INCLUDE"
  367. case MLDv2MulticastAddressRecordTypeModeIsExcluded:
  368. return "MODE_IS_EXCLUDE"
  369. case MLDv2MulticastAddressRecordTypeChangeToIncludeMode:
  370. return "CHANGE_TO_INCLUDE_MODE"
  371. case MLDv2MulticastAddressRecordTypeChangeToExcludeMode:
  372. return "CHANGE_TO_EXCLUDE_MODE"
  373. case MLDv2MulticastAddressRecordTypeAllowNewSources:
  374. return "ALLOW_NEW_SOURCES"
  375. case MLDv2MulticastAddressRecordTypeBlockOldSources:
  376. return "BLOCK_OLD_SOURCES"
  377. default:
  378. return fmt.Sprintf("UNKNOWN(%d)", m)
  379. }
  380. }
  381. // MLDv2MulticastAddressRecord contains information on the sender listening to a
  382. // single multicast address on the interface the report is sent.
  383. // https://tools.ietf.org/html/rfc3810#section-5.2.4
  384. type MLDv2MulticastAddressRecord struct {
  385. // 5.2.5. Record Type
  386. RecordType MLDv2MulticastAddressRecordType
  387. // 5.2.6. Auxiliary Data Length (number of 32-bit words)
  388. AuxDataLen uint8
  389. // 5.2.7. Number Of Sources (N)
  390. N uint16
  391. // 5.2.8. Multicast Address
  392. MulticastAddress net.IP
  393. // 5.2.9 Source Address [i]
  394. SourceAddresses []net.IP
  395. // 5.2.10 Auxiliary Data
  396. AuxiliaryData []byte
  397. }
  398. // decodes a multicast address record from bytes
  399. func (m *MLDv2MulticastAddressRecord) decode(data []byte, df gopacket.DecodeFeedback) (int, error) {
  400. if len(data) < 4 {
  401. df.SetTruncated()
  402. return 0, errors.New(
  403. "Multicast Listener Report Message V2 layer less than 4 bytes for Multicast Address Record")
  404. }
  405. m.RecordType = MLDv2MulticastAddressRecordType(data[0])
  406. m.AuxDataLen = data[1]
  407. m.N = binary.BigEndian.Uint16(data[2:4])
  408. m.MulticastAddress = data[4:20]
  409. for i := uint16(0); i < m.N; i++ {
  410. begin := 20 + (int(i) * 16)
  411. end := begin + 16
  412. if len(data) < end {
  413. df.SetTruncated()
  414. return begin, fmt.Errorf(
  415. "Multicast Listener Report Message V2 layer less than %d bytes for Multicast Address Record", end)
  416. }
  417. m.SourceAddresses = append(m.SourceAddresses, data[begin:end])
  418. }
  419. expectedLengthWithouAuxData := 20 + (int(m.N) * 16)
  420. expectedTotalLength := (int(m.AuxDataLen) * 4) + expectedLengthWithouAuxData // *4 because AuxDataLen are 32bit words
  421. if len(data) < expectedTotalLength {
  422. return expectedLengthWithouAuxData, fmt.Errorf(
  423. "Multicast Listener Report Message V2 layer less than %d bytes for Multicast Address Record",
  424. expectedLengthWithouAuxData)
  425. }
  426. m.AuxiliaryData = data[expectedLengthWithouAuxData:expectedTotalLength]
  427. return expectedTotalLength, nil
  428. }
  429. // String sums this layer up nicely formatted
  430. func (m *MLDv2MulticastAddressRecord) String() string {
  431. return fmt.Sprintf(
  432. "RecordType: %d (%s), AuxDataLen: %d [32-bit words], N: %d, Multicast Address: %s, SourceAddresses: %s, Auxiliary Data: %#x",
  433. m.RecordType,
  434. m.RecordType.String(),
  435. m.AuxDataLen,
  436. m.N,
  437. m.MulticastAddress.To16(),
  438. m.SourceAddresses,
  439. m.AuxiliaryData)
  440. }
  441. // serializes a multicast address record
  442. func (m *MLDv2MulticastAddressRecord) serializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
  443. if err := m.serializeAuxiliaryDataTo(b, opts); err != nil {
  444. return err
  445. }
  446. if err := m.serializeSourceAddressesTo(b, opts); err != nil {
  447. return err
  448. }
  449. buf, err := b.PrependBytes(20)
  450. if err != nil {
  451. return err
  452. }
  453. buf[0] = uint8(m.RecordType)
  454. buf[1] = m.AuxDataLen
  455. binary.BigEndian.PutUint16(buf[2:4], m.N)
  456. ma16 := m.MulticastAddress.To16()
  457. if ma16 == nil {
  458. return fmt.Errorf("invalid multicast address '%s'", m.MulticastAddress)
  459. }
  460. copy(buf[4:20], ma16)
  461. return nil
  462. }
  463. // serializes the auxiliary data of a multicast address record
  464. func (m *MLDv2MulticastAddressRecord) serializeAuxiliaryDataTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
  465. if remainder := len(m.AuxiliaryData) % 4; remainder != 0 {
  466. zeroWord := []byte{0x0, 0x0, 0x0, 0x0}
  467. m.AuxiliaryData = append(m.AuxiliaryData, zeroWord[:remainder]...)
  468. }
  469. if opts.FixLengths {
  470. auxDataLen := len(m.AuxiliaryData) / 4
  471. if auxDataLen > math.MaxUint8 {
  472. return fmt.Errorf("auxilary data is %d 32-bit words, but the maximum is 255 32-bit words", auxDataLen)
  473. }
  474. m.AuxDataLen = uint8(auxDataLen)
  475. }
  476. buf, err := b.PrependBytes(len(m.AuxiliaryData))
  477. if err != nil {
  478. return err
  479. }
  480. copy(buf, m.AuxiliaryData)
  481. return nil
  482. }
  483. // serializes the source addresses of a multicast address record preserving the order
  484. func (m *MLDv2MulticastAddressRecord) serializeSourceAddressesTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
  485. if opts.FixLengths {
  486. numberOfSourceAddresses := len(m.SourceAddresses)
  487. if numberOfSourceAddresses > math.MaxUint16 {
  488. return fmt.Errorf(
  489. "%d source addresses added, but the maximum is 65535",
  490. numberOfSourceAddresses)
  491. }
  492. m.N = uint16(numberOfSourceAddresses)
  493. }
  494. lastItemIdx := len(m.SourceAddresses) - 1
  495. for k := range m.SourceAddresses {
  496. i := lastItemIdx - k // reverse order
  497. buf, err := b.PrependBytes(16)
  498. if err != nil {
  499. return err
  500. }
  501. sa16 := m.SourceAddresses[i].To16()
  502. if sa16 == nil {
  503. return fmt.Errorf("invalid source address [%d] '%s'", i, m.SourceAddresses[i])
  504. }
  505. copy(buf, sa16)
  506. }
  507. return nil
  508. }
  509. func decodeMLDv2MulticastListenerReport(data []byte, p gopacket.PacketBuilder) error {
  510. m := &MLDv2MulticastListenerReportMessage{}
  511. return decodingLayerDecoder(m, data, p)
  512. }
  513. func decodeMLDv2MulticastListenerQuery(data []byte, p gopacket.PacketBuilder) error {
  514. m := &MLDv2MulticastListenerQueryMessage{}
  515. return decodingLayerDecoder(m, data, p)
  516. }