1 // Copyright 2012 Google, Inc. All rights reserved.
2 // Copyright 2009-2011 Andreas Krennmair. All rights reserved.
4 // Use of this source code is governed by a BSD-style license
5 // that can be found in the LICENSE file in the root of the source
16 "github.com/google/gopacket"
22 IGMPMembershipQuery IGMPType = 0x11 // General or group specific query
23 IGMPMembershipReportV1 IGMPType = 0x12 // Version 1 Membership Report
24 IGMPMembershipReportV2 IGMPType = 0x16 // Version 2 Membership Report
25 IGMPLeaveGroup IGMPType = 0x17 // Leave Group
26 IGMPMembershipReportV3 IGMPType = 0x22 // Version 3 Membership Report
29 // String conversions for IGMP message types
30 func (i IGMPType) String() string {
32 case IGMPMembershipQuery:
33 return "IGMP Membership Query"
34 case IGMPMembershipReportV1:
35 return "IGMPv1 Membership Report"
36 case IGMPMembershipReportV2:
37 return "IGMPv2 Membership Report"
38 case IGMPMembershipReportV3:
39 return "IGMPv3 Membership Report"
47 type IGMPv3GroupRecordType uint8
50 IGMPIsIn IGMPv3GroupRecordType = 0x01 // Type MODE_IS_INCLUDE, source addresses x
51 IGMPIsEx IGMPv3GroupRecordType = 0x02 // Type MODE_IS_EXCLUDE, source addresses x
52 IGMPToIn IGMPv3GroupRecordType = 0x03 // Type CHANGE_TO_INCLUDE_MODE, source addresses x
53 IGMPToEx IGMPv3GroupRecordType = 0x04 // Type CHANGE_TO_EXCLUDE_MODE, source addresses x
54 IGMPAllow IGMPv3GroupRecordType = 0x05 // Type ALLOW_NEW_SOURCES, source addresses x
55 IGMPBlock IGMPv3GroupRecordType = 0x06 // Type BLOCK_OLD_SOURCES, source addresses x
58 func (i IGMPv3GroupRecordType) String() string {
61 return "MODE_IS_INCLUDE"
63 return "MODE_IS_EXCLUDE"
65 return "CHANGE_TO_INCLUDE_MODE"
67 return "CHANGE_TO_EXCLUDE_MODE"
69 return "ALLOW_NEW_SOURCES"
71 return "BLOCK_OLD_SOURCES"
77 // IGMP represents an IGMPv3 message.
81 MaxResponseTime time.Duration
84 SupressRouterProcessing bool
86 IntervalTime time.Duration
87 SourceAddresses []net.IP
88 NumberOfGroupRecords uint16
89 NumberOfSources uint16
90 GroupRecords []IGMPv3GroupRecord
91 Version uint8 // IGMP protocol version
94 // IGMPv1or2 stores header details for an IGMPv1 or IGMPv2 packet.
97 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
98 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
99 // | Type | Max Resp Time | Checksum |
100 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103 type IGMPv1or2 struct {
105 Type IGMPType // IGMP message type
106 MaxResponseTime time.Duration // meaningful only in Membership Query messages
107 Checksum uint16 // 16-bit checksum of entire ip payload
108 GroupAddress net.IP // either 0 or an IP multicast address
112 // decodeResponse dissects IGMPv1 or IGMPv2 packet.
113 func (i *IGMPv1or2) decodeResponse(data []byte) error {
115 return errors.New("IGMP packet too small")
118 i.MaxResponseTime = igmpTimeDecode(data[1])
119 i.Checksum = binary.BigEndian.Uint16(data[2:4])
120 i.GroupAddress = net.IP(data[4:8])
126 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
127 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
128 // | Type = 0x22 | Reserved | Checksum |
129 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
130 // | Reserved | Number of Group Records (M) |
131 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
133 // . Group Record [1] .
135 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
137 // . Group Record [2] .
139 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
141 // . Group Record [M] .
143 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
145 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
146 // | Record Type | Aux Data Len | Number of Sources (N) |
147 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
148 // | Multicast Address |
149 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
150 // | Source Address [1] |
152 // | Source Address [2] |
154 // | Source Address [N] |
155 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
157 // . Auxiliary Data .
159 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
161 // IGMPv3GroupRecord stores individual group records for a V3 Membership Report message.
162 type IGMPv3GroupRecord struct {
163 Type IGMPv3GroupRecordType
164 AuxDataLen uint8 // this should always be 0 as per IGMPv3 spec.
165 NumberOfSources uint16
166 MulticastAddress net.IP
167 SourceAddresses []net.IP
168 AuxData uint32 // NOT USED
171 func (i *IGMP) decodeIGMPv3MembershipReport(data []byte) error {
173 return errors.New("IGMPv3 Membership Report too small #1")
176 i.Checksum = binary.BigEndian.Uint16(data[2:4])
177 i.NumberOfGroupRecords = binary.BigEndian.Uint16(data[6:8])
180 for j := 0; j < int(i.NumberOfGroupRecords); j++ {
181 if len(data) < recordOffset+8 {
182 return errors.New("IGMPv3 Membership Report too small #2")
185 var gr IGMPv3GroupRecord
186 gr.Type = IGMPv3GroupRecordType(data[recordOffset])
187 gr.AuxDataLen = data[recordOffset+1]
188 gr.NumberOfSources = binary.BigEndian.Uint16(data[recordOffset+2 : recordOffset+4])
189 gr.MulticastAddress = net.IP(data[recordOffset+4 : recordOffset+8])
191 if len(data) < recordOffset+8+int(gr.NumberOfSources)*4 {
192 return errors.New("IGMPv3 Membership Report too small #3")
195 // append source address records.
196 for i := 0; i < int(gr.NumberOfSources); i++ {
197 sourceAddr := net.IP(data[recordOffset+8+i*4 : recordOffset+12+i*4])
198 gr.SourceAddresses = append(gr.SourceAddresses, sourceAddr)
201 i.GroupRecords = append(i.GroupRecords, gr)
202 recordOffset += 8 + 4*int(gr.NumberOfSources)
208 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
209 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
210 // | Type = 0x11 | Max Resp Code | Checksum |
211 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
213 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
214 // | Resv |S| QRV | QQIC | Number of Sources (N) |
215 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
216 // | Source Address [1] |
218 // | Source Address [2] |
220 // | Source Address [N] |
221 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
223 // decodeIGMPv3MembershipQuery parses the IGMPv3 message of type 0x11
224 func (i *IGMP) decodeIGMPv3MembershipQuery(data []byte) error {
226 return errors.New("IGMPv3 Membership Query too small #1")
229 i.MaxResponseTime = igmpTimeDecode(data[1])
230 i.Checksum = binary.BigEndian.Uint16(data[2:4])
231 i.SupressRouterProcessing = data[8]&0x8 != 0
232 i.GroupAddress = net.IP(data[4:8])
233 i.RobustnessValue = data[8] & 0x7
234 i.IntervalTime = igmpTimeDecode(data[9])
235 i.NumberOfSources = binary.BigEndian.Uint16(data[10:12])
237 if len(data) < 12+int(i.NumberOfSources)*4 {
238 return errors.New("IGMPv3 Membership Query too small #2")
241 for j := 0; j < int(i.NumberOfSources); j++ {
242 i.SourceAddresses = append(i.SourceAddresses, net.IP(data[12+j*4:16+j*4]))
248 // igmpTimeDecode decodes the duration created by the given byte, using the
249 // algorithm in http://www.rfc-base.org/txt/rfc-3376.txt section 4.1.1.
250 func igmpTimeDecode(t uint8) time.Duration {
252 return time.Millisecond * 100 * time.Duration(t)
254 mant := (t & 0x70) >> 4
256 return time.Millisecond * 100 * time.Duration((mant|0x10)<<(exp+3))
259 // LayerType returns LayerTypeIGMP for the V1,2,3 message protocol formats.
260 func (i *IGMP) LayerType() gopacket.LayerType { return LayerTypeIGMP }
261 func (i *IGMPv1or2) LayerType() gopacket.LayerType { return LayerTypeIGMP }
263 func (i *IGMPv1or2) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
265 return errors.New("IGMP Packet too small")
268 i.Type = IGMPType(data[0])
269 i.MaxResponseTime = igmpTimeDecode(data[1])
270 i.Checksum = binary.BigEndian.Uint16(data[2:4])
271 i.GroupAddress = net.IP(data[4:8])
276 func (i *IGMPv1or2) NextLayerType() gopacket.LayerType {
277 return gopacket.LayerTypeZero
280 func (i *IGMPv1or2) CanDecode() gopacket.LayerClass {
284 // DecodeFromBytes decodes the given bytes into this layer.
285 func (i *IGMP) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
287 return errors.New("IGMP packet is too small")
290 // common IGMP header values between versions 1..3 of IGMP specification..
291 i.Type = IGMPType(data[0])
294 case IGMPMembershipQuery:
295 i.decodeIGMPv3MembershipQuery(data)
296 case IGMPMembershipReportV3:
297 i.decodeIGMPv3MembershipReport(data)
299 return errors.New("unsupported IGMP type")
305 // CanDecode returns the set of layer types that this DecodingLayer can decode.
306 func (i *IGMP) CanDecode() gopacket.LayerClass {
310 // NextLayerType returns the layer type contained by this DecodingLayer.
311 func (i *IGMP) NextLayerType() gopacket.LayerType {
312 return gopacket.LayerTypeZero
315 // decodeIGMP will parse IGMP v1,2 or 3 protocols. Checks against the
316 // IGMP type are performed against byte[0], logic then iniitalizes and
317 // passes the appropriate struct (IGMP or IGMPv1or2) to
318 // decodingLayerDecoder.
319 func decodeIGMP(data []byte, p gopacket.PacketBuilder) error {
321 return errors.New("IGMP packet is too small")
324 // byte 0 contains IGMP message type.
325 switch IGMPType(data[0]) {
326 case IGMPMembershipQuery:
327 // IGMPv3 Membership Query payload is >= 12
329 i := &IGMP{Version: 3}
330 return decodingLayerDecoder(i, data, p)
331 } else if len(data) == 8 {
334 i.Version = 1 // IGMPv1 has a query length of 8 and MaxResp = 0
336 i.Version = 2 // IGMPv2 has a query length of 8 and MaxResp != 0
339 return decodingLayerDecoder(i, data, p)
341 case IGMPMembershipReportV3:
342 i := &IGMP{Version: 3}
343 return decodingLayerDecoder(i, data, p)
344 case IGMPMembershipReportV1:
345 i := &IGMPv1or2{Version: 1}
346 return decodingLayerDecoder(i, data, p)
347 case IGMPLeaveGroup, IGMPMembershipReportV2:
348 // leave group and Query Report v2 used in IGMPv2 only.
349 i := &IGMPv1or2{Version: 2}
350 return decodingLayerDecoder(i, data, p)
354 return errors.New("Unable to determine IGMP type.")