-
Notifications
You must be signed in to change notification settings - Fork 0
/
goIGMP_recv_unicast.go
179 lines (137 loc) · 6.35 KB
/
goIGMP_recv_unicast.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
package goIGMP
import (
"context"
"fmt"
"log"
"net"
"net/netip"
"sync"
"time"
"github.com/randomizedcoder/gopacket"
"github.com/randomizedcoder/gopacket/layers"
)
func (r IGMPReporter) recvUnicastIGMP(wg *sync.WaitGroup, ctx context.Context, interf side) {
var (
//err error
localIP netip.Addr
ok bool
)
defer wg.Done()
if localIP, ok = r.NetAddr[interf]; !ok {
log.Fatalf("recvUnicastIGMP(%s) interface IP lookup error", interf)
}
debugLog(r.debugLevel > 10, fmt.Sprintf("recvUnicastIGMP(%s) localIP:%s started", interf, localIP))
forLoop:
for loops := 0; ; loops++ {
select {
case <-ctx.Done():
debugLog(r.debugLevel > 10, fmt.Sprintf("recvIGMP(%s) loops:%d ctx.Done()", interf, loops))
break forLoop
default:
debugLog(r.debugLevel > 100, fmt.Sprintf("recvIGMP(%s) loops:%d ctx is not cancelled", interf, loops))
}
loopStartTime := time.Now()
r.pC.WithLabelValues("recvUnicastIGMP", "loops", "counter").Inc()
debugLog(r.debugLevel > 100, fmt.Sprintf("recvUnicastIGMP(%s) localIP:%s loops:%d", interf, localIP, loops))
err := r.uCon[IN].SetReadDeadline(time.Now().Add(r.conf.SocketReadDeadLine))
if err != nil {
log.Fatal(fmt.Sprintf("recvUnicastIGMP(%s) localIP:%s loops:%d SetReadDeadline err:", interf, localIP, loops), err)
}
buf := bytePool.Get().(*[]byte)
n, addr, err := r.uCon[IN].ReadFrom(*buf)
if err != nil {
if nerr, ok := err.(net.Error); ok && nerr.Timeout() {
debugLog(r.debugLevel > 10, fmt.Sprintf("recvUnicastIGMP(%s) localIP:%s loops:%d ReadFrom timeout", interf, localIP, loops))
r.pC.WithLabelValues("recvUnicastIGMP", "timeout", "counter").Inc()
bytePool.Put(buf)
continue
}
}
packetStartTime := time.Now()
debugLog(r.debugLevel > 10, fmt.Sprintf("recvUnicastIGMP(%s) localIP:%s loops:%d n:%d, addr:%s", interf, localIP, loops, n, addr))
r.pC.WithLabelValues("recvUnicastIGMP", "n", "counter").Add(float64(n))
//------------------
// 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("recvUnicastIGMP(%s) localIP:%s loops:%d type:%s", interf, localIP, loops, igmpType))
r.pC.WithLabelValues("recvUnicastIGMP", 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("recvUnicastIGMP(%s) localIP:%s loops:%d This isn't deserializing to IGMP. Ignoring", interf, localIP, loops))
r.pC.WithLabelValues("recvUnicastIGMP", "deserializing", "error").Inc()
bytePool.Put(buf)
continue
}
// outside interface can change between ethernet/GRE
o, ok := r.IntOutName.Load(interf)
if !ok {
debugLog(r.debugLevel > 10, fmt.Sprintf("recvUnicastIGMP(%s) Load !ok", interf))
r.pC.WithLabelValues("recvUnicastIGMP", "Load", "error").Inc()
bytePool.Put(buf)
continue
}
out, ok := o.(side)
if !ok {
debugLog(r.debugLevel > 10, fmt.Sprintf("recvUnicastIGMP(%s) localIP:%s loops:%d o.(side) type cast error", interf, localIP, loops))
continue
}
// For type1/2 we need to decode to find the group address
switch igmpType {
//case layers.IGMPMembershipQuery:
//TODO implment this
case layers.IGMPMembershipReportV1:
r.sendIGMPv1or2(interf, loops, out, igmpLayer, buf)
case layers.IGMPMembershipReportV2:
r.sendIGMPv1or2(interf, loops, out, igmpLayer, buf)
case layers.IGMPMembershipReportV3:
r.sendIGMPv3(interf, loops, out, buf)
case layers.IGMPLeaveGroup:
r.sendIGMPLeave(interf, loops, out, buf)
default:
debugLog(r.debugLevel > 10, fmt.Sprintf("recvUnicastIGMP(%s) localIP:%s loops:%d unexpected igmp.Type", interf, localIP, loops))
r.pC.WithLabelValues("recvUnicastIGMP", "unexpectedIgmpType", "error").Inc()
}
r.pH.WithLabelValues("recvUnicastIGMP", "sincePacketStartTime", "counter").Observe(time.Since(packetStartTime).Seconds())
r.pH.WithLabelValues("recvUnicastIGMP", "sinceLoopStartTime", "counter").Observe(time.Since(loopStartTime).Seconds())
}
}
// sendIGMPv1or2 needs to send to the multicast destination, so it decodes the payload to find the group
func (r IGMPReporter) sendIGMPv1or2(interf side, loops int, out side, igmpLayer gopacket.Layer, buf *[]byte) {
igmpv1or2, ok := igmpLayer.(*layers.IGMPv1or2)
if !ok {
debugLog(r.debugLevel > 10, fmt.Sprintf("recvUnicastIGMP(%s) loops:%d sendIGMPv1or2 igmpLayer.(*layers.IGMPv1or2) type cast error", interf, loops))
}
debugLog(r.debugLevel > 10, fmt.Sprintf("recvUnicastIGMP(%s) loops:%d sendIGMPv1or2 proxyUniToMultiv1or2 to:%s", interf, loops, out))
r.proxyUniToMultiv1or2(out, igmpv1or2.GroupAddress, buf)
}
// sendIGMPv3 is more simple, and just sends to the IGMPv3 destination 224.0.0.22
func (r IGMPReporter) sendIGMPv3(interf side, loops int, out side, buf *[]byte) {
var dest destIP
if r.conf.UnicastMembershipReports {
debugLog(r.debugLevel > 10, fmt.Sprintf("recvUnicastIGMP(%s) loops:%d sendIGMPv3 UnicastMembershipReports dest = QueryHost", interf, loops))
dest = QueryHost
} else {
debugLog(r.debugLevel > 10, fmt.Sprintf("recvUnicastIGMP(%s) loops:%d sendIGMPv3 dest = IGMPHosts", interf, loops))
dest = IGMPHosts
}
debugLog(r.debugLevel > 10, fmt.Sprintf("recvUnicastIGMP(%s) loops:%d sendIGMPv3 proxying to:%s", interf, loops, out))
r.proxy(out, dest, buf)
bytePool.Put(buf)
}
// sendIGMPv1or2 needs to send to the multicast destination, so it decodes the payload to find the group
func (r IGMPReporter) sendIGMPLeave(interf side, loops int, out side, buf *[]byte) {
debugLog(r.debugLevel > 10, fmt.Sprintf("recvUnicastIGMP(%s) loops:%d sendIGMPLeave proxyUniToMultiv1or2 to:%s", interf, loops, out))
r.proxyUniToMultiv1or2(out, net.IPv4allrouter, buf)
}