forked from antrea-io/libOpenflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathigmp.go
434 lines (403 loc) · 13.7 KB
/
igmp.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
package protocol
import (
"encoding/binary"
"errors"
"fmt"
"net"
)
const (
IGMPQuery = 0x11
IGMPv1Report = 0x12
IGMPv2Report = 0x16
IGMPv2LeaveGroup = 0x17
IGMPv3Report = 0x22
IGMPIsIn = 0x01 // Type MODE_IS_INCLUDE, source addresses x
IGMPIsEx = 0x02 // Type MODE_IS_EXCLUDE, source addresses x
IGMPToIn = 0x03 // Type CHANGE_TO_INCLUDE_MODE, source addresses x
IGMPToEx = 0x04 // Type CHANGE_TO_EXCLUDE_MODE, source addresses x
IGMPAllow = 0x05 // Type ALLOW_NEW_SOURCES, source addresses x
IGMPBlock = 0x06 // Type BLOCK_OLD_SOURCES, source addresses x
)
type IGMPMessage interface {
GetMessageType() uint8
}
// IGMPv1:
//
// 0 1 2 3
// 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
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// |Version| Type | Unused | Checksum |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | Group Address |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//
// IGMPv2:
//
// 0 1 2 3
// 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
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | Type | Max Resp Time | Checksum |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | Group Address |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
type IGMPv1or2 struct {
Type uint8
MaxResponseTime uint8 // It is 0 for IGMPv1 message.
Checksum uint16
GroupAddress net.IP
}
func (p *IGMPv1or2) Len() uint16 {
return 8
}
func (p *IGMPv1or2) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(p.Len()))
n := 0
data[n] = p.Type
n += 1
data[n] = p.MaxResponseTime
n += 1
binary.BigEndian.PutUint16(data[n:], p.Checksum)
n += 2
copy(data[n:n+4], p.GroupAddress.To4())
return
}
func (p *IGMPv1or2) UnmarshalBinary(data []byte) error {
if len(data) < 8 {
return errors.New("The []byte is too short to unmarshal a full IGMPv1or2 message.")
}
p.Type = data[0]
p.MaxResponseTime = data[1]
p.Checksum = binary.BigEndian.Uint16(data[2:4])
p.GroupAddress = make([]byte, 4)
copy(p.GroupAddress, data[4:8])
return nil
}
func (p *IGMPv1or2) GetMessageType() uint8 {
return p.Type
}
func NewIGMPv1Query(group net.IP) *IGMPv1or2 {
return &IGMPv1or2{Type: IGMPQuery, GroupAddress: group}
}
func NewIGMPv1Report(group net.IP) *IGMPv1or2 {
return &IGMPv1or2{Type: IGMPv1Report, GroupAddress: group}
}
func NewIGMPv2Query(group net.IP, maxResponseTime uint8) *IGMPv1or2 {
return &IGMPv1or2{
Type: IGMPQuery,
MaxResponseTime: maxResponseTime,
GroupAddress: group,
}
}
func NewIGMPv2Report(group net.IP) *IGMPv1or2 {
return &IGMPv1or2{
Type: IGMPv2Report,
GroupAddress: group,
}
}
func NewIGMPv2Leave(group net.IP) *IGMPv1or2 {
return &IGMPv1or2{
Type: IGMPv2LeaveGroup,
GroupAddress: group,
}
}
// IGMPv3Query:
//
// 0 1 2 3
// 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
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | Type = 0x11 | Max Resp Code | Checksum |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | Group Address |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | Resv |S| QRV | QQIC | Number of Sources (N) |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | Source Address [1] |
// +- -+
// | Source Address [2] |
// +- . -+
// . . .
// . . .
// +- -+
// | Source Address [N] |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
type IGMPv3Query struct {
Type uint8
MaxResponseTime uint8
Checksum uint16
GroupAddress net.IP
Reserved uint8
SuppressRouterProcessing bool
RobustnessValue uint8
IntervalTime uint8
NumberOfSources uint16
SourceAddresses []net.IP
}
func (p *IGMPv3Query) Len() uint16 {
return 12 + p.NumberOfSources*4
}
func (p *IGMPv3Query) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(p.Len()))
n := 0
data[n] = p.Type
n += 1
data[n] = p.MaxResponseTime
n += 1
binary.BigEndian.PutUint16(data[n:], p.Checksum)
n += 2
copy(data[n:n+4], p.GroupAddress.To4())
n += 4
sBit := uint8(0x0)
if p.SuppressRouterProcessing {
sBit = 0x8
}
data[n] = sBit | p.RobustnessValue&0x7
n += 1
data[n] = p.IntervalTime
n += 1
binary.BigEndian.PutUint16(data[n:], p.NumberOfSources)
n += 2
for _, src := range p.SourceAddresses {
copy(data[n:n+4], src.To4())
n += 4
}
return
}
func (p *IGMPv3Query) UnmarshalBinary(data []byte) error {
if len(data) < 12 {
return errors.New("The []byte is too short to unmarshal a full IGMPv3Query message.")
}
n := 0
p.Type = data[n]
n += 1
p.MaxResponseTime = data[n]
n += 1
p.Checksum = binary.BigEndian.Uint16(data[n:])
n += 2
p.GroupAddress = make([]byte, 4)
copy(p.GroupAddress, data[n:n+4])
n += 4
p.SuppressRouterProcessing = data[n]&0x8 != 0
p.RobustnessValue = data[n] & 0x7
n += 1
p.IntervalTime = data[n]
n += 1
p.NumberOfSources = binary.BigEndian.Uint16(data[n:])
n += 2
if len(data) < int(p.Len()) {
return fmt.Errorf("The []byte is too short to unmarshal a full IGMPv3Query message.")
}
for j := 0; j < int(p.NumberOfSources); j++ {
p.SourceAddresses = append(p.SourceAddresses, data[n:n+4])
n += 4
}
return nil
}
func (p *IGMPv3Query) GetMessageType() uint8 {
return IGMPQuery
}
func NewIGMPv3Query(group net.IP, maxResponseTime uint8, queryInterval uint8, sources []net.IP) *IGMPv3Query {
return &IGMPv3Query{
Type: IGMPQuery,
MaxResponseTime: maxResponseTime,
GroupAddress: group,
IntervalTime: queryInterval,
NumberOfSources: uint16(len(sources)),
SourceAddresses: sources,
}
}
// IGMPv3GroupRecord:
//
// 0 1 2 3
// 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
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | Record Type | Aux Data Len | Number of Sources (N) |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | Multicast Address |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | Source Address [1] |
// +- -+
// | Source Address [2] |
// +- -+
// . . .
// . . .
// . . .
// +- -+
// | Source Address [N] |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | |
// . .
// . Auxiliary Data .
// . .
// | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
type IGMPv3GroupRecord struct {
Type uint8
AuxDataLen uint8 // this should always be 0 as per IGMPv3 spec.
NumberOfSources uint16
MulticastAddress net.IP
SourceAddresses []net.IP
AuxData []uint32 // NOT USED
}
func (p *IGMPv3GroupRecord) Len() uint16 {
return 8 + uint16(p.AuxDataLen)*4 + p.NumberOfSources*4
}
func (p *IGMPv3GroupRecord) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(p.Len()))
n := 0
data[n] = p.Type
n += 1
data[n] = p.AuxDataLen
n += 1
binary.BigEndian.PutUint16(data[n:], p.NumberOfSources)
n += 2
copy(data[n:n+4], p.MulticastAddress.To4())
n += 4
for _, src := range p.SourceAddresses {
copy(data[n:n+4], src.To4())
n += 4
}
for _, d := range p.AuxData {
binary.BigEndian.PutUint32(data[n:], d)
n += 4
}
return
}
func (p *IGMPv3GroupRecord) UnmarshalBinary(data []byte) error {
if len(data) < 8 {
return errors.New("The []byte is too short to unmarshal a full IGMPv3GroupRecord message.")
}
n := 0
p.Type = data[n]
n += 1
p.AuxDataLen = data[n]
n += 1
p.NumberOfSources = binary.BigEndian.Uint16(data[n:])
n += 2
p.MulticastAddress = make([]byte, 4)
copy(p.MulticastAddress, data[n:n+4])
n += 4
if len(data) < int(p.Len()) {
return fmt.Errorf("The []byte is too short to unmarshal a full IGMPv3GroupRecord message.")
}
for i := uint16(0); i < p.NumberOfSources; i++ {
p.SourceAddresses = append(p.SourceAddresses, data[n:n+4])
n += 4
}
for i := uint8(0); i < p.AuxDataLen; i++ {
p.AuxData = append(p.AuxData, binary.BigEndian.Uint32(data[n:]))
n += 4
}
return nil
}
func NewGroupRecord(recordType uint8, group net.IP, sources []net.IP) IGMPv3GroupRecord {
return IGMPv3GroupRecord{
Type: recordType,
MulticastAddress: group,
NumberOfSources: uint16(len(sources)),
SourceAddresses: sources,
}
}
// IGMPv3MembershipReport:
//
// 0 1 2 3
// 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
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | Type = 0x22 | Reserved | Checksum |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | Reserved | Number of Group Records (M) |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | |
// . .
// . Group Record [1] .
// . .
// | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | |
// . .
// . Group Record [2] .
// . .
// | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | . |
// . . .
// | . |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | |
// . .
// . Group Record [M] .
// . .
// | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
type IGMPv3MembershipReport struct {
Type uint8
Reserved uint8
Checksum uint16
Reserved2 uint16
NumberOfGroups uint16
GroupRecords []IGMPv3GroupRecord
}
func (p *IGMPv3MembershipReport) Len() uint16 {
length := uint16(8)
for _, r := range p.GroupRecords {
length += r.Len()
}
return length
}
func (p *IGMPv3MembershipReport) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(p.Len()))
n := 0
data[n] = p.Type
n += 1
// Length for field "Reserved"
n += 1
binary.BigEndian.PutUint16(data[n:], p.Checksum)
n += 2
// Length for field "Reserved2".
n += 2
binary.BigEndian.PutUint16(data[n:], p.NumberOfGroups)
n += 2
for _, r := range p.GroupRecords {
b, err := r.MarshalBinary()
if err != nil {
return nil, err
}
copy(data[n:], b)
n += int(r.Len())
}
return
}
func (p *IGMPv3MembershipReport) UnmarshalBinary(data []byte) error {
if len(data) < 8 {
return errors.New("The []byte is too short to unmarshal a full IGMPv3MembershipReport message.")
}
n := 0
p.Type = data[n]
n += 1
// Length for field "Reserved"
n += 1
p.Checksum = binary.BigEndian.Uint16(data[n:])
n += 2
// Length for field "Reserved2".
n += 2
p.NumberOfGroups = binary.BigEndian.Uint16(data[n:])
n += 2
for i := uint16(0); i < p.NumberOfGroups; i++ {
gr := new(IGMPv3GroupRecord)
if err := gr.UnmarshalBinary(data[n:]); err != nil {
return err
}
p.GroupRecords = append(p.GroupRecords, *gr)
n += int(gr.Len())
}
return nil
}
func (p *IGMPv3MembershipReport) GetMessageType() uint8 {
return IGMPv3Report
}
func NewIGMPv3Report(groups []IGMPv3GroupRecord) *IGMPv3MembershipReport {
return &IGMPv3MembershipReport{
Type: IGMPv3Report,
NumberOfGroups: uint16(len(groups)),
GroupRecords: groups,
}
}