blob: 75f2759d915e091dcf0f2b3d21b1ef46369ac5ea [file] [log] [blame]
Arjun E K57a7fcb2020-01-30 06:44:45 +00001/*
2 * Copyright 2018-present Open Networking Foundation
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 * http://www.apache.org/licenses/LICENSE-2.0
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS,
9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and
11 * limitations under the License.
12 */
13
14package igmp
15
16import (
17 "encoding/binary"
Anand S Katti09541352020-01-29 15:54:01 +053018 "net"
19 "time"
20
Arjun E K57a7fcb2020-01-30 06:44:45 +000021 "github.com/google/gopacket"
22 "github.com/google/gopacket/layers"
23 bbsim "github.com/opencord/bbsim/internal/bbsim/types"
24 omci "github.com/opencord/omci-sim"
25 "github.com/opencord/voltha-protos/v2/go/openolt"
26 log "github.com/sirupsen/logrus"
Arjun E K57a7fcb2020-01-30 06:44:45 +000027)
28
29func SendIGMPLeaveGroupV2(ponPortId uint32, onuId uint32, serialNumber string, portNo uint32, macAddress net.HardwareAddr, stream bbsim.Stream) error {
30 log.WithFields(log.Fields{
31 "OnuId": onuId,
32 "SerialNumber": serialNumber,
33 "PortNo": portNo,
34 }).Debugf("Entered SendIGMPLeaveGroupV2")
35 igmp := createIGMPV2LeaveRequestPacket()
36 pkt, err := serializeIgmpPacket(ponPortId, onuId, macAddress, igmp)
37
38 if err != nil {
39 log.WithFields(log.Fields{
40 "OnuId": onuId,
41 "IntfId": ponPortId,
42 "SerialNumber": serialNumber,
43 }).Errorf("Seriliazation of igmp packet failed : %s", err)
44 return err
45 }
46
47 gemid, err := omci.GetGemPortId(ponPortId, onuId)
48 if err != nil {
49 log.WithFields(log.Fields{
50 "OnuId": onuId,
51 "IntfId": ponPortId,
52 "SerialNumber": serialNumber,
53 }).Errorf("Can't retrieve GemPortId for IGMP: %s", err)
54 return err
55 }
56
57 data := &openolt.Indication_PktInd{
58 PktInd: &openolt.PacketIndication{
59 IntfType: "pon",
60 IntfId: ponPortId,
61 GemportId: uint32(gemid),
62 Pkt: pkt,
63 PortNo: portNo,
64 },
65 }
66 //Sending IGMP packets
67 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Matteo Scandolo583f17d2020-02-13 10:35:17 -080068 log.WithFields(log.Fields{
69 "OnuId": onuId,
70 "SerialNumber": serialNumber,
71 "PortNo": portNo,
72 "IntfId": ponPortId,
73 "err": err,
74 }).Error("Fail to send IGMP PktInd indication for ONU")
Arjun E K57a7fcb2020-01-30 06:44:45 +000075 return err
76 }
77 return nil
78}
79
80func SendIGMPMembershipReportV2(ponPortId uint32, onuId uint32, serialNumber string, portNo uint32, macAddress net.HardwareAddr, stream bbsim.Stream) error {
81 log.WithFields(log.Fields{
82 "OnuId": onuId,
83 "SerialNumber": serialNumber,
84 "PortNo": portNo,
85 }).Debugf("Entered SendIGMPMembershipReportV2")
86 igmp := createIGMPV2MembershipReportPacket()
87 pkt, err := serializeIgmpPacket(ponPortId, onuId, macAddress, igmp)
88
89 if err != nil {
90 log.WithFields(log.Fields{
91 "OnuId": onuId,
92 "IntfId": ponPortId,
93 "SerialNumber": serialNumber,
94 }).Errorf("Seriliazation of igmp packet failed : %s", err)
95 return err
96 }
97
98 gemid, err := omci.GetGemPortId(ponPortId, onuId)
99 if err != nil {
100 log.WithFields(log.Fields{
101 "OnuId": onuId,
102 "IntfId": ponPortId,
103 "SerialNumber": serialNumber,
104 }).Errorf("Can't retrieve GemPortId for IGMP: %s", err)
105 return err
106 }
107
108 data := &openolt.Indication_PktInd{
109 PktInd: &openolt.PacketIndication{
110 IntfType: "pon",
111 IntfId: ponPortId,
112 GemportId: uint32(gemid),
113 Pkt: pkt,
114 PortNo: portNo,
115 },
116 }
117 //Sending IGMP packets
118 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
Matteo Scandolo583f17d2020-02-13 10:35:17 -0800119 log.WithFields(log.Fields{
120 "OnuId": onuId,
121 "SerialNumber": serialNumber,
122 "PortNo": portNo,
123 "IntfId": ponPortId,
124 "err": err,
125 }).Errorf("Fail to send IGMP PktInd indication")
Arjun E K57a7fcb2020-01-30 06:44:45 +0000126 return err
127 }
128 return nil
129}
130
Arjun E Kdd443f02020-02-07 15:24:01 +0000131func SendIGMPMembershipReportV3(ponPortId uint32, onuId uint32, serialNumber string, portNo uint32, macAddress net.HardwareAddr, stream bbsim.Stream) error {
Anand S Katti09541352020-01-29 15:54:01 +0530132 log.WithFields(log.Fields{
133 "OnuId": onuId,
134 "SerialNumber": serialNumber,
135 "PortNo": portNo,
136 }).Debugf("Entered SendIGMPMembershipReportV3")
137 igmp := createIGMPV3MembershipReportPacket()
138 pkt, err := serializeIgmpPacket(ponPortId, onuId, macAddress, igmp)
Arjun E Kdd443f02020-02-07 15:24:01 +0000139
Anand S Katti09541352020-01-29 15:54:01 +0530140 if err != nil {
141 log.WithFields(log.Fields{
142 "OnuId": onuId,
143 "IntfId": ponPortId,
144 "SerialNumber": serialNumber,
145 }).Errorf("Seriliazation of igmp packet failed : %s", err)
146 return err
147 }
Arjun E Kdd443f02020-02-07 15:24:01 +0000148
Anand S Katti09541352020-01-29 15:54:01 +0530149 gemid, err := omci.GetGemPortId(ponPortId, onuId)
150 if err != nil {
151 log.WithFields(log.Fields{
152 "OnuId": onuId,
153 "IntfId": ponPortId,
154 "SerialNumber": serialNumber,
155 }).Errorf("Can't retrieve GemPortId for IGMP: %s", err)
156 return err
157 }
Arjun E Kdd443f02020-02-07 15:24:01 +0000158
Anand S Katti09541352020-01-29 15:54:01 +0530159 data := &openolt.Indication_PktInd{
160 PktInd: &openolt.PacketIndication{
161 IntfType: "pon",
162 IntfId: ponPortId,
163 GemportId: uint32(gemid),
164 Pkt: pkt,
165 PortNo: portNo,
166 },
167 }
168 //Sending IGMP packets
169 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
170 log.Errorf("Fail to send IGMP PktInd indication for ONU: %s, IntfId: %s, SerialNumber: %s, error: %v", onuId, ponPortId, serialNumber, err)
171 return err
172 }
173 return nil
Arjun E Kdd443f02020-02-07 15:24:01 +0000174}
175
176func createIGMPV3MembershipReportPacket() IGMP {
177
Anand S Katti09541352020-01-29 15:54:01 +0530178 groupRecord1 := IGMPv3GroupRecord{
179 Type: IGMPv3GroupRecordType(IGMPIsIn),
180 AuxDataLen: 0, // this should always be 0 as per IGMPv3 spec.
181 NumberOfSources: 3,
182 MulticastAddress: net.IPv4(224, 0, 0, 22),
183 SourceAddresses: []net.IP{net.IPv4(15, 14, 20, 24), net.IPv4(15, 14, 20, 26), net.IPv4(15, 14, 20, 25)},
184 AuxData: 0, // NOT USED
185 }
Arjun E Kdd443f02020-02-07 15:24:01 +0000186
Anand S Katti09541352020-01-29 15:54:01 +0530187 groupRecord2 := IGMPv3GroupRecord{
188 Type: IGMPv3GroupRecordType(IGMPIsIn),
189 AuxDataLen: 0, // this should always be 0 as per IGMPv3 spec.
190 NumberOfSources: 2,
191 MulticastAddress: net.IPv4(224, 0, 0, 25),
192 SourceAddresses: []net.IP{net.IPv4(15, 14, 20, 30), net.IPv4(15, 14, 20, 31)},
193 AuxData: 0, // NOT USED
194 }
Arjun E Kdd443f02020-02-07 15:24:01 +0000195
Anand S Katti09541352020-01-29 15:54:01 +0530196 igmpDefault := IGMP{
197 Type: 0x22, //IGMPV3 Membership Report
198 MaxResponseTime: time.Duration(1),
199 Checksum: 0,
200 GroupAddress: net.IPv4(224, 0, 0, 22),
201 SupressRouterProcessing: false,
202 RobustnessValue: 0,
203 IntervalTime: time.Duration(1),
204 SourceAddresses: []net.IP{net.IPv4(224, 0, 0, 24)},
205 NumberOfGroupRecords: 2,
206 NumberOfSources: 1,
207 GroupRecords: []IGMPv3GroupRecord{groupRecord1, groupRecord2},
208 Version: 3,
209 }
Arjun E Kdd443f02020-02-07 15:24:01 +0000210
Anand S Katti09541352020-01-29 15:54:01 +0530211 return igmpDefault
Arjun E Kdd443f02020-02-07 15:24:01 +0000212}
213
Arjun E K57a7fcb2020-01-30 06:44:45 +0000214//func serializeIgmpPacket(intfId uint32, onuId uint32, srcMac net.HardwareAddr, igmp *layers.IGMP) ([]byte, error) {
215func createIGMPV2MembershipReportPacket() IGMP {
216 return IGMP{
217 Type: 0x16, //IGMPV2 Membership Report
218 MaxResponseTime: time.Duration(1),
219 Checksum: 0,
220 GroupAddress: net.IPv4(224, 0, 0, 22),
221 Version: 2,
222 }
223}
224
225func createIGMPV2LeaveRequestPacket() IGMP {
226 return IGMP{
227 Type: 0x17, //IGMPV2 Leave Group
228 MaxResponseTime: time.Duration(1),
229 Checksum: 0,
230 GroupAddress: net.IPv4(224, 0, 0, 22),
231 Version: 2,
232 }
233}
234
235func serializeIgmpPacket(intfId uint32, onuId uint32, srcMac net.HardwareAddr, igmp IGMP) ([]byte, error) {
236 buffer := gopacket.NewSerializeBuffer()
237 options := gopacket.SerializeOptions{
238 ComputeChecksums: true,
239 FixLengths: true,
240 }
241
242 ethernetLayer := &layers.Ethernet{
243 SrcMAC: srcMac,
244 DstMAC: net.HardwareAddr{0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
245 EthernetType: layers.EthernetTypeIPv4,
246 }
247
248 ipLayer := &layers.IPv4{
249 Version: 4,
250 TOS: 0x10,
251 Id: 0,
252 TTL: 128,
253 SrcIP: []byte{0, 0, 0, 0},
254 DstIP: []byte{224, 0, 0, 22},
255 Protocol: layers.IPProtocolIGMP,
256 Options: []layers.IPv4Option{{OptionType: 148, OptionLength: 4, OptionData: make([]byte, 0)}}, //Adding router alert option
257 }
258
259 if err := gopacket.SerializeLayers(buffer, options, ethernetLayer, ipLayer, igmp); err != nil {
260 return nil, err
261 }
262
263 return buffer.Bytes(), nil
264}
265
266//-----------------------------------------***********************---------------------------------
267// BaseLayer is a convenience struct which implements the LayerData and
268// LayerPayload functions of the Layer interface.
269type BaseLayer struct {
270 // Contents is the set of bytes that make up this layer. IE: for an
271 // Ethernet packet, this would be the set of bytes making up the
272 // Ethernet frame.
273 Contents []byte
274 // Payload is the set of bytes contained by (but not part of) this
275 // Layer. Again, to take Ethernet as an example, this would be the
276 // set of bytes encapsulated by the Ethernet protocol.
277 Payload []byte
278}
279
280type IGMPType uint8
281
282type IGMP struct {
283 BaseLayer
284 Type IGMPType
285 MaxResponseTime time.Duration
286 Checksum uint16
287 GroupAddress net.IP
288 SupressRouterProcessing bool
289 RobustnessValue uint8
290 IntervalTime time.Duration
291 SourceAddresses []net.IP
292 NumberOfGroupRecords uint16
293 NumberOfSources uint16
Anand S Katti09541352020-01-29 15:54:01 +0530294 GroupRecords []IGMPv3GroupRecord
Arjun E K57a7fcb2020-01-30 06:44:45 +0000295 Version uint8 // IGMP protocol version
296}
297
Arjun E Kdd443f02020-02-07 15:24:01 +0000298// IGMPv3GroupRecord stores individual group records for a V3 Membership Report message.
299type IGMPv3GroupRecord struct {
Anand S Katti09541352020-01-29 15:54:01 +0530300 Type IGMPv3GroupRecordType
301 AuxDataLen uint8 // this should always be 0 as per IGMPv3 spec.
302 NumberOfSources uint16
303 MulticastAddress net.IP
304 SourceAddresses []net.IP
305 AuxData uint32 // NOT USED
Arjun E Kdd443f02020-02-07 15:24:01 +0000306}
307
308type IGMPv3GroupRecordType uint8
309
310const (
Anand S Katti09541352020-01-29 15:54:01 +0530311 IGMPIsIn IGMPv3GroupRecordType = 0x01 // Type MODE_IS_INCLUDE, source addresses x
312 IGMPIsEx IGMPv3GroupRecordType = 0x02 // Type MODE_IS_EXCLUDE, source addresses x
313 IGMPToIn IGMPv3GroupRecordType = 0x03 // Type CHANGE_TO_INCLUDE_MODE, source addresses x
314 IGMPToEx IGMPv3GroupRecordType = 0x04 // Type CHANGE_TO_EXCLUDE_MODE, source addresses x
315 IGMPAllow IGMPv3GroupRecordType = 0x05 // Type ALLOW_NEW_SOURCES, source addresses x
316 IGMPBlock IGMPv3GroupRecordType = 0x06 // Type BLOCK_OLD_SOURCES, source addresses x
Arjun E Kdd443f02020-02-07 15:24:01 +0000317)
318
319func (i IGMPv3GroupRecordType) String() string {
Anand S Katti09541352020-01-29 15:54:01 +0530320 switch i {
321 case IGMPIsIn:
322 return "MODE_IS_INCLUDE"
323 case IGMPIsEx:
324 return "MODE_IS_EXCLUDE"
325 case IGMPToIn:
326 return "CHANGE_TO_INCLUDE_MODE"
327 case IGMPToEx:
328 return "CHANGE_TO_EXCLUDE_MODE"
329 case IGMPAllow:
330 return "ALLOW_NEW_SOURCES"
331 case IGMPBlock:
332 return "BLOCK_OLD_SOURCES"
333 default:
334 return ""
335 }
Arjun E Kdd443f02020-02-07 15:24:01 +0000336}
337
Arjun E K57a7fcb2020-01-30 06:44:45 +0000338// SerializeTo writes the serialized form of this layer into the
339// SerializationBuffer, implementing gopacket.SerializableLayer.
340// See the docs for gopacket.SerializableLayer for more info.
Arjun E K57a7fcb2020-01-30 06:44:45 +0000341func (igmp IGMP) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
Anand S Katti09541352020-01-29 15:54:01 +0530342 log.Debugf("Serializing IGMP Packet")
Arjun E K57a7fcb2020-01-30 06:44:45 +0000343 data, err := b.PrependBytes(8915)
Anand S Katti09541352020-01-29 15:54:01 +0530344 if err != nil {
345 return err
346 }
347 if igmp.Version == 2 {
348 data[0] = byte(igmp.Type)
349 data[1] = byte(igmp.MaxResponseTime)
350 data[2] = 0
351 data[3] = 0
352 copy(data[4:8], igmp.GroupAddress.To4())
353 if opts.ComputeChecksums {
354 igmp.Checksum = tcpipChecksum(data, 0)
355 binary.BigEndian.PutUint16(data[2:4], igmp.Checksum)
356 }
357 } else if igmp.Version == 3 {
Arjun E K57a7fcb2020-01-30 06:44:45 +0000358
Anand S Katti09541352020-01-29 15:54:01 +0530359 data[0] = byte(igmp.Type)
360 data[1] = 0
361 data[2] = 0
362 data[3] = 0
363 data[4] = 0
364 data[5] = 0
365 binary.BigEndian.PutUint16(data[6:8], igmp.NumberOfGroupRecords)
366 j := 8
367 for i := uint16(0); i < igmp.NumberOfGroupRecords; i++ {
368 data[j] = byte(igmp.GroupRecords[i].Type)
369 data[j+1] = byte(0)
370 binary.BigEndian.PutUint16(data[j+2:j+4], igmp.GroupRecords[i].NumberOfSources)
371 copy(data[j+4:j+8], igmp.GroupRecords[i].MulticastAddress.To4())
372 j = j + 8
373 for m := uint16(0); m < igmp.GroupRecords[i].NumberOfSources; m++ {
374 copy(data[j:(j+4)], igmp.GroupRecords[i].SourceAddresses[m].To4())
375 j = j + 4
376 }
377 }
378 if opts.ComputeChecksums {
379 igmp.Checksum = tcpipChecksum(data, 0)
380 binary.BigEndian.PutUint16(data[2:4], igmp.Checksum)
381 }
382 }
383 return nil
Arjun E K57a7fcb2020-01-30 06:44:45 +0000384}
385
386// Calculate the TCP/IP checksum defined in rfc1071. The passed-in csum is any
387// initial checksum data that's already been computed.
388func tcpipChecksum(data []byte, csum uint32) uint16 {
389 // to handle odd lengths, we loop to length - 1, incrementing by 2, then
390 // handle the last byte specifically by checking against the original
391 // length.
392 length := len(data) - 1
393 for i := 0; i < length; i += 2 {
394 // For our test packet, doing this manually is about 25% faster
395 // (740 ns vs. 1000ns) than doing it by calling binary.BigEndian.Uint16.
396 csum += uint32(data[i]) << 8
397 csum += uint32(data[i+1])
398 }
399 if len(data)%2 == 1 {
400 csum += uint32(data[length]) << 8
401 }
402 for csum > 0xffff {
403 csum = (csum >> 16) + (csum & 0xffff)
404 }
405 return ^uint16(csum)
406}
407
408func (IGMP) LayerType() gopacket.LayerType { return layers.LayerTypeIGMP }