-
Notifications
You must be signed in to change notification settings - Fork 0
/
goIGMP_recv.go
338 lines (275 loc) · 12.8 KB
/
goIGMP_recv.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
package goIGMP
import (
"context"
"fmt"
"log"
"net"
"reflect"
"sync"
"time"
"github.com/randomizedcoder/gopacket"
"github.com/randomizedcoder/gopacket/layers"
)
var (
bytePool = sync.Pool{
New: func() interface{} {
b := make([]byte, maxIGMPPacketRecieveBytesCst)
return &b
},
}
)
const (
ignoreNonActiveInterfaceModulusCst = 100
)
func (r IGMPReporter) recvIGMP(wg *sync.WaitGroup, ctx context.Context, interf side, g destIP) {
defer wg.Done()
debugLog(r.debugLevel > 10, fmt.Sprintf("recvIGMP(%s) g:%s started", interf, r.mapIPtoNetAddr[g]))
forLoop:
for loops := 0; ; loops++ {
select {
case <-ctx.Done():
debugLog(r.debugLevel > 10, fmt.Sprintf("recvIGMP(%s) g:%s loops:%d ctx.Done()", interf, r.mapIPtoNetAddr[g], loops))
break forLoop
default:
debugLog(r.debugLevel > 100, fmt.Sprintf("recvIGMP(%s) g:%s loops:%d ctx is not cancelled", interf, r.mapIPtoNetAddr[g], loops))
}
loopStartTime := time.Now()
r.pCrecvIGMP.WithLabelValues("loop", interf.String(), r.mapIPtoNetAddr[g].String(), "counter").Inc()
debugLog(r.debugLevel > 100, fmt.Sprintf("recvIGMP(%s) g:%s loops:%d", interf, r.mapIPtoNetAddr[g], loops))
err := r.mConIGMP[interf][r.mapIPtoNetAddr[g]].SetReadDeadline(time.Now().Add(r.conf.SocketReadDeadLine))
if err != nil {
log.Fatal(fmt.Sprintf("recvIGMP(%s) g:%s loops:%d SetReadDeadline err:", interf, r.mapIPtoNetAddr[g], loops), err)
}
buf := bytePool.Get().(*[]byte)
n, cm, src, err := r.mConIGMP[interf][r.mapIPtoNetAddr[g]].ReadFrom(*buf)
if err != nil {
if nerr, ok := err.(net.Error); ok && nerr.Timeout() {
debugLog(r.debugLevel > 10, fmt.Sprintf("recvIGMP(%s) g:%s ReadFrom timeout", interf, r.mapIPtoNetAddr[g]))
r.pCrecvIGMP.WithLabelValues("timeout", interf.String(), r.mapIPtoNetAddr[g].String(), "counter").Inc()
bytePool.Put(buf)
continue
}
r.pCrecvIGMP.WithLabelValues("ReadFrom", interf.String(), r.mapIPtoNetAddr[g].String(), "error").Inc()
bytePool.Put(buf)
continue
}
packetStartTime := time.Now()
r.pCrecvIGMP.WithLabelValues("n", interf.String(), r.mapIPtoNetAddr[g].String(), "counter").Add(float64(n))
//------------------
// Ignore traffic on the non-active outside interface
if r.AltOutExists {
if r.ignoreOnNonActiveOutOrAltInterface(&interf) {
if loops%ignoreNonActiveInterfaceModulusCst == 0 {
debugLog(r.debugLevel > 10,
fmt.Sprintf("recvIGMP(%s) g:%s loops:%d ignoring on non active outside interface",
interf, r.mapIPtoNetAddr[g], loops))
}
bytePool.Put(buf)
continue
}
}
//------------------
// Validate incoming interface is correct
// https://pkg.go.dev/golang.org/x/net/ipv4#ControlMessage
// https://pkg.go.dev/net#Interface
if r.NetIFIndex[cm.IfIndex] != interf {
debugLog(r.debugLevel > 100, fmt.Sprintf("recvIGMP(%s) g:%s loops:%d r.NetIFIndex[%d]:%s != interf:%s. Packet not for our interface. Ignoring",
interf, r.mapIPtoNetAddr[g], loops, cm.IfIndex, r.NetIFIndex[cm.IfIndex], interf))
r.pCrecvIGMP.WithLabelValues("interf", interf.String(), r.mapIPtoNetAddr[g].String(), "ignore").Inc()
bytePool.Put(buf)
continue
}
debugLog(r.debugLevel > 100, fmt.Sprintf("recvIGMP(%s) g:%s loops:%d n:%d, cm:%s, src:%s", interf, r.mapIPtoNetAddr[g], loops, n, cm, src))
if r.debugLevel > 100 {
if !cm.Dst.IsMulticast() {
debugLog(r.debugLevel > 10, fmt.Sprintf("recvIGMP(%s) g:%s loops:%d not multicast. ??! warning", interf, r.mapIPtoNetAddr[g], loops))
}
}
// check this is not from our own interface IP
if reflect.DeepEqual(src, r.NetIP[interf]) {
debugLog(r.debugLevel > 10, fmt.Sprintf(
"recvIGMP(%s) g:%s loops:%d src:%s is ourself:%s. Ignoring", interf, r.mapIPtoNetAddr[g], loops, src.String(), r.NetIP[interf].String()))
r.pCrecvIGMP.WithLabelValues("srcSelf", interf.String(), r.mapIPtoNetAddr[g].String(), "ignore").Inc()
bytePool.Put(buf)
continue
}
//------------------
// Validate destination IP is correct
dstAddr, err := r.netip2Addr(cm.Dst)
if err != nil {
log.Fatal(fmt.Sprintf("recvIGMP(%s) g:%s loops:%d mapNetAddrtoIP err:", interf, r.mapIPtoNetAddr[g], loops), err)
r.pCrecvIGMP.WithLabelValues("netip2Addr", interf.String(), r.mapIPtoNetAddr[g].String(), "error").Inc()
}
if dstAddr != r.mapIPtoNetAddr[g] {
debugLog(r.debugLevel > 100, fmt.Sprintf("recvIGMP(%s) g:%s loops:%d Packet not for our multicast group. Ignoring", interf, r.mapIPtoNetAddr[g], loops))
r.pCrecvIGMP.WithLabelValues("dstAddr", interf.String(), r.mapIPtoNetAddr[g].String(), "ignore").Inc()
bytePool.Put(buf)
continue
}
//------------------
// Validate this is IGMP and it's the correct type of IGMP
// type IGMPType uint8
// const (
// IGMPMembershipQuery IGMPType = 0x11 // General or group specific query
// IGMPMembershipReportV1 IGMPType = 0x12 // Version 1 Membership Report
// IGMPMembershipReportV2 IGMPType = 0x16 // Version 2 Membership Report
// IGMPLeaveGroup IGMPType = 0x17 // Leave Group
// IGMPMembershipReportV3 IGMPType = 0x22 // Version 3 Membership Report
// )
// https://github.com/randomizedcoder/gopacket/blob/master/layers/igmp.go#L18C1-L27C2
igmpType := layers.IGMPType((*buf)[0])
debugLog(r.debugLevel > 10, fmt.Sprintf("recvIGMP(%s) g:%s loops:%d type:%s", interf, r.mapIPtoNetAddr[g], loops, igmpType))
r.pC.WithLabelValues("recvIGMP", igmpType.String(), "count").Inc()
// https://pkg.go.dev/github.com/tsg/gopacket#hdr-Basic_Usage
// https://github.com/randomizedcoder/gopacket/blob/master/layers/igmp.go#L224
packet := gopacket.NewPacket(*buf, layers.LayerTypeIGMP, gopacket.Default)
igmpLayer := packet.Layer(layers.LayerTypeIGMP)
if igmpLayer == nil {
debugLog(r.debugLevel > 10, fmt.Sprintf("recvIGMP(%s) g:%s loops:%d This isn't deserializing to IGMP. Ignoring", interf, r.mapIPtoNetAddr[g], loops))
r.pCrecvIGMP.WithLabelValues("deserializing", interf.String(), r.mapIPtoNetAddr[g].String(), "error").Inc()
bytePool.Put(buf)
continue
}
switch igmpType {
case layers.IGMPMembershipQuery:
r.pCrecvIGMP.WithLabelValues("IGMPMembershipQuery", interf.String(), r.mapIPtoNetAddr[g].String(), "counter").Inc()
srcIP, err := r.netip2Addr(cm.Src)
if err != nil {
r.pCrecvIGMP.WithLabelValues("srcNetip2Addr", interf.String(), r.mapIPtoNetAddr[g].String(), "error").Inc()
}
r.querierSourceIP = srcIP
if r.conf.QueryNotify {
select {
case r.QueryNotifyCh <- struct{}{}:
r.pCrecvIGMP.WithLabelValues("QueryNotifyCh", interf.String(), r.mapIPtoNetAddr[g].String(), "counter").Inc()
debugLog(r.debugLevel > 10, fmt.Sprintf("recvIGMP(%s) g:%s loops:%d QueryNotifyCh <- struct{}{}", interf, r.mapIPtoNetAddr[g], loops))
default:
r.pCrecvIGMP.WithLabelValues("QueryNotifyCh", interf.String(), r.mapIPtoNetAddr[g].String(), "error").Inc()
debugLog(r.debugLevel > 10, fmt.Sprintf("recvIGMP(%s) g:%s loops:%d QueryNotifyCh failed. Channel full? Is something reading from the channel?", interf, r.mapIPtoNetAddr[g], loops))
}
}
case layers.IGMPMembershipReportV2:
r.pCrecvIGMP.WithLabelValues("IGMPMembershipReportV2", interf.String(), r.mapIPtoNetAddr[g].String(), "counter").Inc()
igmpv1or2, okC := igmpLayer.(*layers.IGMPv1or2)
if !okC {
debugLog(r.debugLevel > 10, fmt.Sprintf("recvIGMP(%s) g:%s loops:%d type cast error igmpLayer.(*layers.IGMPv1or2)", interf, r.mapIPtoNetAddr[g], loops))
r.pC.WithLabelValues("recvIGMP", "cast", "error").Inc()
}
na, err := r.netip2Addr(igmpv1or2.GroupAddress)
if err != nil {
log.Fatal("recvIGMP netip2Addr(sa) err", err)
}
var mi MembershipItem
mi.Group = na
mitems := []MembershipItem{mi}
if r.conf.MembershipReportsFromNetwork {
select {
case r.MembershipReportFromNetworkCh <- mitems:
r.pCrecvIGMP.WithLabelValues("MembershipReportFromNetworkCh", interf.String(), r.mapIPtoNetAddr[g].String(), "counter").Inc()
debugLog(r.debugLevel > 10, fmt.Sprintf("recvIGMP(%s) g:%s loops:%d MembershipReportFromNetworkCh", interf, r.mapIPtoNetAddr[g], loops))
default:
r.pCrecvIGMP.WithLabelValues("MembershipReportFromNetworkCh", interf.String(), r.mapIPtoNetAddr[g].String(), "error").Inc()
debugLog(r.debugLevel > 10, fmt.Sprintf("recvIGMP(%s) g:%s loops:%d MembershipReportFromNetworkCh failed. Channel full? Is something reading from the channel?", interf, r.mapIPtoNetAddr[g], loops))
}
}
case layers.IGMPMembershipReportV3:
igmp, ok := igmpLayer.(*layers.IGMP)
if !ok {
debugLog(r.debugLevel > 10, fmt.Sprintf("recvIGMP(%s) g:%s loops:%d type cast error igmpLayer.(*layers.IGMP)", interf, r.mapIPtoNetAddr[g], loops))
r.pCrecvIGMP.WithLabelValues("typeCast", interf.String(), r.mapIPtoNetAddr[g].String(), "error").Inc()
bytePool.Put(buf)
continue
}
mitems := r.IGMPv3GroupRecordsToMembershipItem(igmp.GroupRecords)
if r.conf.MembershipReportsFromNetwork {
select {
case r.MembershipReportFromNetworkCh <- mitems:
r.pCrecvIGMP.WithLabelValues("MembershipReportFromNetworkCh", interf.String(), r.mapIPtoNetAddr[g].String(), "counter").Inc()
debugLog(r.debugLevel > 10, fmt.Sprintf("recvIGMP(%s) g:%s loops:%d MembershipReportFromNetworkCh", interf, r.mapIPtoNetAddr[g], loops))
default:
r.pCrecvIGMP.WithLabelValues("MembershipReportFromNetworkCh", interf.String(), r.mapIPtoNetAddr[g].String(), "error").Inc()
debugLog(r.debugLevel > 10, fmt.Sprintf("recvIGMP(%s) g:%s loops:%d MembershipReportFromNetworkCh failed. Channel full? Is something reading from the channel?", interf, r.mapIPtoNetAddr[g], loops))
}
}
//case layers.IGMPLeaveGroup:
// TODO handle leave
default:
r.pCrecvIGMP.WithLabelValues("WrongType", interf.String(), r.mapIPtoNetAddr[g].String(), "error").Inc()
debugLog(r.debugLevel > 10, fmt.Sprintf("recvIGMP(%s) g:%s loops:%d This shouldn't happen. Bug?", interf, r.mapIPtoNetAddr[g], loops))
}
if r.proxyIt(interf) {
r.pCrecvIGMP.WithLabelValues("proxyIt", interf.String(), r.mapIPtoNetAddr[g].String(), "counter").Inc()
r.pCrecvIGMP.WithLabelValues("proxyIt", interf.String(), r.mapIPtoNetAddr[g].String(), "bytes").Add(float64(len(*buf)))
out, ok := r.IntOutName.Load(interf)
if !ok {
debugLog(r.debugLevel > 10, fmt.Sprintf("recvIGMP(%s) Load !ok", interf))
r.pC.WithLabelValues("recvIGMP", "Load", "error").Inc()
bytePool.Put(buf)
continue
}
debugLog(r.debugLevel > 10, fmt.Sprintf("recvIGMP(%s) g:%s loops:%d proxying to:%s", interf, r.mapIPtoNetAddr[g], loops, out.(side)))
r.proxy(out.(side), g, buf)
bytePool.Put(buf)
}
r.pHrecvIGMP.WithLabelValues("sincePacketStartTime", interf.String(), r.mapIPtoNetAddr[g].String(), "counter").Observe(time.Since(packetStartTime).Seconds())
r.pHrecvIGMP.WithLabelValues("sinceLoopStartTime", interf.String(), r.mapIPtoNetAddr[g].String(), "counter").Observe(time.Since(loopStartTime).Seconds())
}
}
func (r IGMPReporter) proxyIt(interf side) (proxyIt bool) {
switch interf {
case OUT:
if r.conf.ProxyOutToIn {
proxyIt = true
}
case IN:
if r.conf.ProxyInToOut {
proxyIt = true
}
}
return proxyIt
}
func (r IGMPReporter) ignoreOnNonActiveOutOrAltInterface(interf *side) (ignore bool) {
if r.OutsideInterfaces[*interf] {
out, ok := r.IntOutName.Load(IN)
if !ok {
debugLog(r.debugLevel > 10, fmt.Sprintf("ignoreOnNonActiveOutOrAltInterface(%s) Load !ok", *interf))
r.pC.WithLabelValues("ignoreOnNonActiveOutOrAltInterface", "Load", "error").Inc()
ignore = true
return ignore
}
if *interf != out.(side) {
ignore = true
r.pC.WithLabelValues("ignoreOnNonActiveOutOrAltInterface", "ignore", "count").Inc()
debugLog(r.debugLevel > 100, fmt.Sprintf("ignoreOnNonActiveOutOrAltInterface(%s) ignoring non-active outside interface", *interf))
}
}
return ignore
}
// IGMPv3GroupRecordsToMembershipItem converts the real IGMP packet group memberships
// into the internal representatino as a list of []membershipItem
func (r IGMPReporter) IGMPv3GroupRecordsToMembershipItem(groupRecords []layers.IGMPv3GroupRecord) (mitems []MembershipItem) {
startTime := time.Now()
defer func() {
r.pH.WithLabelValues("IGMPv3GroupRecordsToMembershipItem", "start", "complete").Observe(time.Since(startTime).Seconds())
}()
r.pC.WithLabelValues("IGMPv3GroupRecordsToMembershipItem", "start", "count").Inc()
debugLog(r.debugLevel > 100, "IGMPv3GroupRecordsToMembershipItem()")
for _, gr := range groupRecords {
var g MembershipItem
for _, sa := range gr.SourceAddresses {
na, err := r.netip2Addr(sa)
if err != nil {
log.Fatal("recvIGMP netip2Addr(sa) err", err)
}
g.Sources = append(g.Sources, na)
}
na, err := r.netip2Addr(gr.MulticastAddress)
if err != nil {
log.Fatal("recvIGMP netip2Addr(sa) err", err)
}
g.Group = na
mitems = append(mitems, g)
}
debugLog(r.debugLevel > 10, fmt.Sprintf("groupRecordsToMembershipItem() len(mitems):%d", len(mitems)))
return mitems
}