blob: f3c7e728de880a9ae67655d92d14efaae5ab815b [file] [log] [blame]
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package dhcp
18
19import (
Matteo Scandolo40e067f2019-10-16 16:59:41 -070020 "context"
Matteo Scandolo8d281372020-09-03 16:23:37 -070021 "encoding/hex"
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070022 "errors"
23 "fmt"
24 "github.com/google/gopacket"
25 "github.com/google/gopacket/layers"
26 "github.com/looplab/fsm"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070027 "github.com/opencord/bbsim/internal/bbsim/packetHandlers"
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070028 bbsim "github.com/opencord/bbsim/internal/bbsim/types"
Matteo Scandolo4f4ac792020-10-01 16:33:21 -070029 "github.com/opencord/voltha-protos/v4/go/openolt"
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070030 log "github.com/sirupsen/logrus"
Matteo Scandolo24a88c42020-09-17 14:55:28 -070031 "net"
32 "reflect"
33 "strconv"
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070034)
35
36var dhcpLogger = log.WithFields(log.Fields{
37 "module": "DHCP",
38})
39
40var defaultParamsRequestList = []layers.DHCPOpt{
41 layers.DHCPOptSubnetMask,
42 layers.DHCPOptBroadcastAddr,
43 layers.DHCPOptTimeOffset,
44 layers.DHCPOptRouter,
45 layers.DHCPOptDomainName,
46 layers.DHCPOptDNS,
47 layers.DHCPOptDomainSearch,
48 layers.DHCPOptHostname,
49 layers.DHCPOptNetBIOSTCPNS,
50 layers.DHCPOptNetBIOSTCPScope,
51 layers.DHCPOptInterfaceMTU,
52 layers.DHCPOptClasslessStaticRoute,
53 layers.DHCPOptNTPServers,
54}
55
Matteo Scandolo24a88c42020-09-17 14:55:28 -070056func macAddressToTxId(mac net.HardwareAddr) uint32 {
57
58 // NOTE we want to generate a unique XID,
59 // the easiest way is to transform the macAddress (already unique) into an integer
60 str := ""
61 for _, i := range mac {
62 str = str + fmt.Sprintf("%d", i)
63 }
64 xid, err := strconv.Atoi(str)
Matteo Scandolofe9ac252019-10-25 11:40:17 -070065 if err != nil {
66 log.Fatal("Can't generate unique XID for ONU")
67 }
68
Matteo Scandolo24a88c42020-09-17 14:55:28 -070069 return uint32(xid)
70}
71
72func createDefaultDHCPReq(mac net.HardwareAddr) layers.DHCPv4 {
73 xid := macAddressToTxId(mac)
74
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070075 return layers.DHCPv4{
76 Operation: layers.DHCPOpRequest,
77 HardwareType: layers.LinkTypeEthernet,
78 HardwareLen: 6,
79 HardwareOpts: 0,
Matteo Scandolofe9ac252019-10-25 11:40:17 -070080 Xid: uint32(xid),
Matteo Scandolo40e067f2019-10-16 16:59:41 -070081 ClientHWAddr: mac,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070082 }
83}
84
Matteo Scandolobfcfbb32020-09-28 14:14:39 -070085func createDefaultOpts(gemPortId uint32, intfId uint32, onuId uint32) []layers.DHCPOption {
86 hostname := []byte(fmt.Sprintf("%d.%d.%d.bbsim.onf.org", gemPortId, intfId, onuId))
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070087 opts := []layers.DHCPOption{}
88 opts = append(opts, layers.DHCPOption{
89 Type: layers.DHCPOptHostname,
90 Data: hostname,
91 Length: uint8(len(hostname)),
92 })
93
94 bytes := []byte{}
95 for _, option := range defaultParamsRequestList {
96 bytes = append(bytes, byte(option))
97 }
98
99 opts = append(opts, layers.DHCPOption{
100 Type: layers.DHCPOptParamsRequest,
101 Data: bytes,
102 Length: uint8(len(bytes)),
103 })
104 return opts
105}
106
Matteo Scandolo24a88c42020-09-17 14:55:28 -0700107func createDHCPDisc(intfId uint32, onuId uint32, gemPort uint32, macAddress net.HardwareAddr) *layers.DHCPv4 {
108 dhcpLayer := createDefaultDHCPReq(macAddress)
Matteo Scandolobfcfbb32020-09-28 14:14:39 -0700109 defaultOpts := createDefaultOpts(gemPort, intfId, onuId)
Shrey Baid688b4242020-07-10 20:40:10 +0530110 dhcpLayer.Options = append([]layers.DHCPOption{{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700111 Type: layers.DHCPOptMessageType,
112 Data: []byte{byte(layers.DHCPMsgTypeDiscover)},
113 Length: 1,
114 }}, defaultOpts...)
115
Matteo Scandolobfcfbb32020-09-28 14:14:39 -0700116 data := []byte{01}
117 data = append(data, macAddress...)
Matteo Scandolofe9ac252019-10-25 11:40:17 -0700118 dhcpLayer.Options = append(dhcpLayer.Options, layers.DHCPOption{
119 Type: layers.DHCPOptClientID,
120 Data: data,
121 Length: uint8(len(data)),
122 })
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700123
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700124 return &dhcpLayer
125}
126
Matteo Scandolobfcfbb32020-09-28 14:14:39 -0700127func createDHCPReq(intfId uint32, onuId uint32, macAddress net.HardwareAddr, offeredIp net.IP, gemPortId uint32) *layers.DHCPv4 {
Matteo Scandolo24a88c42020-09-17 14:55:28 -0700128 dhcpLayer := createDefaultDHCPReq(macAddress)
Matteo Scandolobfcfbb32020-09-28 14:14:39 -0700129 defaultOpts := createDefaultOpts(gemPortId, intfId, onuId)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700130
131 dhcpLayer.Options = append(defaultOpts, layers.DHCPOption{
132 Type: layers.DHCPOptMessageType,
133 Data: []byte{byte(layers.DHCPMsgTypeRequest)},
134 Length: 1,
135 })
136
137 data := []byte{182, 21, 0, 128}
138 dhcpLayer.Options = append(dhcpLayer.Options, layers.DHCPOption{
139 Type: layers.DHCPOptServerID,
140 Data: data,
141 Length: uint8(len(data)),
142 })
143
144 data = []byte{0xcd, 0x28, 0xcb, 0xcc, 0x00, 0x01, 0x00, 0x01,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700145 0x23, 0xed, 0x11, 0xec, 0x4e, 0xfc, 0xcd, 0x28, byte(intfId), byte(onuId)}
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700146 dhcpLayer.Options = append(dhcpLayer.Options, layers.DHCPOption{
147 Type: layers.DHCPOptClientID,
148 Data: data,
149 Length: uint8(len(data)),
150 })
151
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700152 // NOTE we should not request a specific IP, or we should request the one that has been offered
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700153 dhcpLayer.Options = append(dhcpLayer.Options, layers.DHCPOption{
154 Type: layers.DHCPOptRequestIP,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700155 Data: offeredIp,
156 Length: uint8(len(offeredIp)),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700157 })
158 return &dhcpLayer
159}
160
Matteo Scandolo8d281372020-09-03 16:23:37 -0700161func serializeDHCPPacket(intfId uint32, onuId uint32, cTag int, srcMac net.HardwareAddr, dhcp *layers.DHCPv4, pbit uint8) ([]byte, error) {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700162 buffer := gopacket.NewSerializeBuffer()
Matteo Scandoloe0e9b772020-02-03 14:23:43 -0800163
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700164 options := gopacket.SerializeOptions{
165 ComputeChecksums: true,
166 FixLengths: true,
167 }
168
169 ethernetLayer := &layers.Ethernet{
170 SrcMAC: srcMac,
171 DstMAC: net.HardwareAddr{0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
172 EthernetType: layers.EthernetTypeIPv4,
173 }
174
175 ipLayer := &layers.IPv4{
176 Version: 4,
177 TOS: 0x10,
178 TTL: 128,
179 SrcIP: []byte{0, 0, 0, 0},
180 DstIP: []byte{255, 255, 255, 255},
181 Protocol: layers.IPProtocolUDP,
182 }
183
184 udpLayer := &layers.UDP{
185 SrcPort: 68,
186 DstPort: 67,
187 }
188
Shrey Baid688b4242020-07-10 20:40:10 +0530189 _ = udpLayer.SetNetworkLayerForChecksum(ipLayer)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700190 if err := gopacket.SerializeLayers(buffer, options, ethernetLayer, ipLayer, udpLayer, dhcp); err != nil {
Matteo Scandoloe0e9b772020-02-03 14:23:43 -0800191 dhcpLogger.Error("SerializeLayers")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700192 return nil, err
193 }
194
Matteo Scandoloe0e9b772020-02-03 14:23:43 -0800195 untaggedPkt := gopacket.NewPacket(buffer.Bytes(), layers.LayerTypeEthernet, gopacket.Default)
196
Matteo Scandolo8d281372020-09-03 16:23:37 -0700197 taggedPkt, err := packetHandlers.PushSingleTag(cTag, untaggedPkt, pbit)
Matteo Scandoloe0e9b772020-02-03 14:23:43 -0800198 if err != nil {
199 dhcpLogger.Error("TagPacket")
200 return nil, err
201 }
202
203 return gopacket.Payload(taggedPkt.Data()), nil
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700204}
205
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700206func GetDhcpLayer(pkt gopacket.Packet) (*layers.DHCPv4, error) {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700207 layerDHCP := pkt.Layer(layers.LayerTypeDHCPv4)
208 dhcp, _ := layerDHCP.(*layers.DHCPv4)
209 if dhcp == nil {
210 return nil, errors.New("Failed-to-extract-DHCP-layer")
211 }
212 return dhcp, nil
213}
214
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700215func GetDhcpMessageType(dhcp *layers.DHCPv4) (layers.DHCPMsgType, error) {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700216 for _, option := range dhcp.Options {
217 if option.Type == layers.DHCPOptMessageType {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700218 if reflect.DeepEqual(option.Data, []byte{byte(layers.DHCPMsgTypeDiscover)}) {
219 return layers.DHCPMsgTypeDiscover, nil
220 } else if reflect.DeepEqual(option.Data, []byte{byte(layers.DHCPMsgTypeOffer)}) {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700221 return layers.DHCPMsgTypeOffer, nil
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700222 } else if reflect.DeepEqual(option.Data, []byte{byte(layers.DHCPMsgTypeRequest)}) {
223 return layers.DHCPMsgTypeRequest, nil
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700224 } else if reflect.DeepEqual(option.Data, []byte{byte(layers.DHCPMsgTypeAck)}) {
225 return layers.DHCPMsgTypeAck, nil
226 } else if reflect.DeepEqual(option.Data, []byte{byte(layers.DHCPMsgTypeRelease)}) {
227 return layers.DHCPMsgTypeRelease, nil
228 } else {
229 msg := fmt.Sprintf("This type %x is not supported", option.Data)
230 return 0, errors.New(msg)
231 }
232 }
233 }
234 return 0, errors.New("Failed to extract MsgType from dhcp")
235}
236
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700237// returns the DHCP Layer type or error if it's not a DHCP Packet
238func GetDhcpPacketType(pkt gopacket.Packet) (string, error) {
239 dhcpLayer, err := GetDhcpLayer(pkt)
240 if err != nil {
241 return "", err
242 }
243 dhcpMessageType, err := GetDhcpMessageType(dhcpLayer)
244 if err != nil {
245 return "", err
246 }
247
248 return dhcpMessageType.String(), nil
249}
250
Matteo Scandolo4a036262020-08-17 15:56:13 -0700251func sendDHCPPktIn(msg bbsim.ByteMsg, portNo uint32, gemPortId uint32, stream bbsim.Stream) error {
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700252
Matteo Scandolo8d281372020-09-03 16:23:37 -0700253 dhcpLogger.WithFields(log.Fields{
Shrey Baid688b4242020-07-10 20:40:10 +0530254 "OnuId": msg.OnuId,
255 "IntfId": msg.IntfId,
Matteo Scandolo4a036262020-08-17 15:56:13 -0700256 "GemPort": gemPortId,
Shrey Baid688b4242020-07-10 20:40:10 +0530257 "Type": "DHCP",
Matteo Scandolo8d281372020-09-03 16:23:37 -0700258 "Pkt": hex.EncodeToString(msg.Bytes),
259 }).Trace("Sending DHCP packet in")
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700260
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700261 data := &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{
Matteo Scandolo27428702019-10-11 16:21:16 -0700262 IntfType: "pon",
263 IntfId: msg.IntfId,
Matteo Scandolo4a036262020-08-17 15:56:13 -0700264 GemportId: gemPortId,
Matteo Scandolo27428702019-10-11 16:21:16 -0700265 Pkt: msg.Bytes,
266 PortNo: portNo,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700267 }}
268
269 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
270 dhcpLogger.Errorf("Fail to send DHCP PktInd indication. %v", err)
271 return err
272 }
273 return nil
274}
275
Matteo Scandolo24a88c42020-09-17 14:55:28 -0700276func sendDHCPRequest(ponPortId uint32, onuId uint32, serviceName string, serialNumber string, portNo uint32,
Matteo Scandolo8d281372020-09-03 16:23:37 -0700277 cTag int, gemPortId uint32, onuStateMachine *fsm.FSM, onuHwAddress net.HardwareAddr,
278 offeredIp net.IP, pbit uint8, stream bbsim.Stream) error {
Matteo Scandolobfcfbb32020-09-28 14:14:39 -0700279 dhcp := createDHCPReq(ponPortId, onuId, onuHwAddress, offeredIp, gemPortId)
Matteo Scandolo8d281372020-09-03 16:23:37 -0700280 pkt, err := serializeDHCPPacket(ponPortId, onuId, cTag, onuHwAddress, dhcp, pbit)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700281
282 if err != nil {
Matteo Scandolofe9ac252019-10-25 11:40:17 -0700283 dhcpLogger.WithFields(log.Fields{
284 "OnuId": onuId,
285 "IntfId": ponPortId,
286 "OnuSn": serialNumber,
287 "OfferedIp": offeredIp.String(),
288 }).Errorf("Cannot serializeDHCPPacket: %s", err)
289 if err := updateDhcpFailed(onuId, ponPortId, serialNumber, onuStateMachine); err != nil {
290 return err
291 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700292 return err
293 }
294 // NOTE I don't think we need to tag the packet
295 //taggedPkt, err := packetHandlers.PushSingleTag(cTag, pkt)
296
297 msg := bbsim.ByteMsg{
298 IntfId: ponPortId,
299 OnuId: onuId,
300 Bytes: pkt,
301 }
302
Matteo Scandolo4a036262020-08-17 15:56:13 -0700303 if err := sendDHCPPktIn(msg, portNo, gemPortId, stream); err != nil {
Matteo Scandolofe9ac252019-10-25 11:40:17 -0700304 dhcpLogger.WithFields(log.Fields{
305 "OnuId": onuId,
306 "IntfId": ponPortId,
307 "OnuSn": serialNumber,
308 }).Errorf("Cannot sendDHCPPktIn: %s", err)
309 if err := updateDhcpFailed(onuId, ponPortId, serialNumber, onuStateMachine); err != nil {
310 return err
311 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700312 return err
313 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700314
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700315 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700316 "OnuId": onuId,
317 "IntfId": ponPortId,
318 "OnuSn": serialNumber,
319 "OfferedIp": offeredIp.String(),
320 "ServiceName": serviceName,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700321 }).Infof("DHCPRequest Sent")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700322 return nil
323}
324
Matteo Scandolo075b1892019-10-07 12:11:07 -0700325func updateDhcpFailed(onuId uint32, ponPortId uint32, serialNumber string, onuStateMachine *fsm.FSM) error {
326 if err := onuStateMachine.Event("dhcp_failed"); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700327 dhcpLogger.WithFields(log.Fields{
328 "OnuId": onuId,
329 "IntfId": ponPortId,
330 "OnuSn": serialNumber,
Matteo Scandolo075b1892019-10-07 12:11:07 -0700331 }).Errorf("Error while transitioning ONU State %v", err)
332 return err
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700333 }
Matteo Scandolo075b1892019-10-07 12:11:07 -0700334 return nil
335}
336
Matteo Scandolo24a88c42020-09-17 14:55:28 -0700337func SendDHCPDiscovery(ponPortId uint32, onuId uint32, serviceName string, cTag int, gemPortId uint32,
Matteo Scandolo8d281372020-09-03 16:23:37 -0700338 serialNumber string, portNo uint32, stateMachine *fsm.FSM, onuHwAddress net.HardwareAddr,
339 pbit uint8, stream bbsim.Stream) error {
340
Matteo Scandolo24a88c42020-09-17 14:55:28 -0700341 dhcp := createDHCPDisc(ponPortId, onuId, gemPortId, onuHwAddress)
Matteo Scandolo8d281372020-09-03 16:23:37 -0700342 pkt, err := serializeDHCPPacket(ponPortId, onuId, cTag, onuHwAddress, dhcp, pbit)
Matteo Scandoloe0e9b772020-02-03 14:23:43 -0800343
Matteo Scandolo075b1892019-10-07 12:11:07 -0700344 if err != nil {
Matteo Scandolofe9ac252019-10-25 11:40:17 -0700345 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700346 "OnuId": onuId,
347 "IntfId": ponPortId,
348 "OnuSn": serialNumber,
349 "ServiceName": serviceName,
Matteo Scandolofe9ac252019-10-25 11:40:17 -0700350 }).Errorf("Cannot serializeDHCPPacket: %s", err)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700351 if err := updateDhcpFailed(onuId, ponPortId, serialNumber, stateMachine); err != nil {
Matteo Scandolofe9ac252019-10-25 11:40:17 -0700352 return err
353 }
Matteo Scandolo075b1892019-10-07 12:11:07 -0700354 return err
355 }
356 // NOTE I don't think we need to tag the packet
357 //taggedPkt, err := packetHandlers.PushSingleTag(cTag, pkt)
358
359 msg := bbsim.ByteMsg{
360 IntfId: ponPortId,
361 OnuId: onuId,
362 Bytes: pkt,
363 }
364
Matteo Scandolo4a036262020-08-17 15:56:13 -0700365 if err := sendDHCPPktIn(msg, portNo, gemPortId, stream); err != nil {
Matteo Scandolofe9ac252019-10-25 11:40:17 -0700366 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700367 "OnuId": onuId,
368 "IntfId": ponPortId,
369 "OnuSn": serialNumber,
370 "ServiceName": serviceName,
Matteo Scandolofe9ac252019-10-25 11:40:17 -0700371 }).Errorf("Cannot sendDHCPPktIn: %s", err)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700372 if err := updateDhcpFailed(onuId, ponPortId, serialNumber, stateMachine); err != nil {
Matteo Scandolo075b1892019-10-07 12:11:07 -0700373 return err
374 }
375 return err
376 }
377 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700378 "OnuId": onuId,
379 "IntfId": ponPortId,
380 "OnuSn": serialNumber,
381 "ServiceName": serviceName,
382 }).Info("DHCPDiscovery Sent")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700383
Matteo Scandolo4a036262020-08-17 15:56:13 -0700384 if err := stateMachine.Event("dhcp_discovery_sent"); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700385 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700386 "OnuId": onuId,
387 "IntfId": ponPortId,
388 "OnuSn": serialNumber,
389 "ServiceName": serviceName,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700390 }).Errorf("Error while transitioning ONU State %v", err)
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700391 return err
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700392 }
Matteo Scandolo075b1892019-10-07 12:11:07 -0700393 return nil
394}
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700395
Matteo Scandolo24a88c42020-09-17 14:55:28 -0700396func HandleNextPacket(onuId uint32, ponPortId uint32, serviceName string, serialNumber string, portNo uint32,
Matteo Scandolo8d281372020-09-03 16:23:37 -0700397 cTag int, gemPortId uint32, onuHwAddress net.HardwareAddr, onuStateMachine *fsm.FSM,
398 pkt gopacket.Packet, pbit uint8, stream bbsim.Stream) error {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700399
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700400 dhcpLayer, err := GetDhcpLayer(pkt)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700401 if err != nil {
402 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700403 "OnuId": onuId,
404 "IntfId": ponPortId,
405 "OnuSn": serialNumber,
406 "ServiceName": serviceName,
Matteo Scandolo075b1892019-10-07 12:11:07 -0700407 }).Errorf("Can't get DHCP Layer from Packet: %v", err)
408 if err := updateDhcpFailed(onuId, ponPortId, serialNumber, onuStateMachine); err != nil {
409 return err
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700410 }
Matteo Scandolo075b1892019-10-07 12:11:07 -0700411 return err
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700412 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700413 dhcpMessageType, err := GetDhcpMessageType(dhcpLayer)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700414 if err != nil {
415 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700416 "OnuId": onuId,
417 "IntfId": ponPortId,
418 "OnuSn": serialNumber,
419 "ServiceName": serviceName,
Matteo Scandolo075b1892019-10-07 12:11:07 -0700420 }).Errorf("Can't get DHCP Message Type from DHCP Layer: %v", err)
421 if err := updateDhcpFailed(onuId, ponPortId, serialNumber, onuStateMachine); err != nil {
422 return err
423 }
424 return err
425 }
426
427 if dhcpLayer.Operation == layers.DHCPOpReply {
428 if dhcpMessageType == layers.DHCPMsgTypeOffer {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700429 offeredIp := dhcpLayer.YourClientIP
Matteo Scandolo24a88c42020-09-17 14:55:28 -0700430 if err := sendDHCPRequest(ponPortId, onuId, serviceName, serialNumber, portNo, cTag, gemPortId, onuStateMachine, onuHwAddress, offeredIp, pbit, stream); err != nil {
Matteo Scandolo075b1892019-10-07 12:11:07 -0700431 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700432 "OnuId": onuId,
433 "IntfId": ponPortId,
434 "OnuSn": serialNumber,
435 "ServiceName": serviceName,
Matteo Scandolo075b1892019-10-07 12:11:07 -0700436 }).Errorf("Can't send DHCP Request: %s", err)
437 if err := updateDhcpFailed(onuId, ponPortId, serialNumber, onuStateMachine); err != nil {
438 return err
439 }
440 return err
441 }
442 if err := onuStateMachine.Event("dhcp_request_sent"); err != nil {
443 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700444 "OnuId": onuId,
445 "IntfId": ponPortId,
446 "OnuSn": serialNumber,
447 "ServiceName": serviceName,
448 }).Errorf("Error while transitioning State %v", err)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700449 }
450
451 } else if dhcpMessageType == layers.DHCPMsgTypeAck {
452 // NOTE once the ack is received we don't need to do anything but change the state
453 if err := onuStateMachine.Event("dhcp_ack_received"); err != nil {
454 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700455 "OnuId": onuId,
456 "IntfId": ponPortId,
457 "OnuSn": serialNumber,
458 "ServiceName": serviceName,
459 }).Errorf("Error while transitioning State %v", err)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700460 }
461 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700462 "OnuId": onuId,
463 "IntfId": ponPortId,
464 "OnuSn": serialNumber,
465 "ServiceName": serviceName,
Matteo Scandolo075b1892019-10-07 12:11:07 -0700466 }).Infof("DHCP State machine completed")
467 }
468 // NOTE do we need to care about DHCPMsgTypeRelease??
469 } else {
470 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700471 "OnuId": onuId,
472 "IntfId": ponPortId,
473 "OnuSn": serialNumber,
474 "ServiceName": serviceName,
Matteo Scandolo075b1892019-10-07 12:11:07 -0700475 }).Warnf("Unsupported DHCP Operation: %s", dhcpLayer.Operation.String())
476 }
Matteo Scandolofe9ac252019-10-25 11:40:17 -0700477
Matteo Scandolo075b1892019-10-07 12:11:07 -0700478 return nil
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700479}
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700480
481// This method handle the BBR DHCP Packets
482// BBR does not need to do anything but forward the packets in the correct direction
Matteo Scandolo4a036262020-08-17 15:56:13 -0700483func HandleNextBbrPacket(onuId uint32, ponPortId uint32, serialNumber string, doneChannel chan bool, pkt gopacket.Packet, client openolt.OpenoltClient) error {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700484
485 // check if the packet is going:
486 // - outgouing: toward the DHCP
487 // - incoming: toward the ONU
488 isIncoming := packetHandlers.IsIncomingPacket(pkt)
Matteo Scandoloe0e9b772020-02-03 14:23:43 -0800489 dhcpLogger.Tracef("Is Incoming: %t", isIncoming)
490
491 pkt, err := packetHandlers.PopSingleTag(pkt)
492 if err != nil {
493 dhcpLogger.WithFields(log.Fields{
494 "OnuId": onuId,
495 "IntfId": ponPortId,
496 "OnuSn": serialNumber,
497 "error": err,
498 }).Fatalf("Can't untag packet")
499 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700500
501 dhcpType, err := GetDhcpPacketType(pkt)
502 if err != nil {
Matteo Scandoloe0e9b772020-02-03 14:23:43 -0800503 dhcpLogger.WithFields(log.Fields{
504 "OnuId": onuId,
505 "IntfId": ponPortId,
506 "OnuSn": serialNumber,
507 "error": err,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700508 }).Fatalf("Can't find DHCP type for packet")
509 }
510
511 srcMac, _ := packetHandlers.GetSrcMacAddressFromPacket(pkt)
512 dstMac, _ := packetHandlers.GetDstMacAddressFromPacket(pkt)
513
Shrey Baid688b4242020-07-10 20:40:10 +0530514 if isIncoming {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700515
516 onuPacket := openolt.OnuPacket{
517 IntfId: ponPortId,
518 OnuId: onuId,
519 PortNo: onuId,
520 GemportId: 1,
521 Pkt: pkt.Data(),
522 }
523
524 if _, err := client.OnuPacketOut(context.Background(), &onuPacket); err != nil {
Matteo Scandoloe0e9b772020-02-03 14:23:43 -0800525 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700526 "OnuId": onuId,
527 "IntfId": ponPortId,
528 "OnuSn": serialNumber,
529 "Type": dhcpType,
530 "error": err,
531 }).Error("Failed to send DHCP packet to the ONU")
532 }
533
Matteo Scandoloe0e9b772020-02-03 14:23:43 -0800534 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700535 "OnuId": onuId,
536 "IntfId": ponPortId,
537 "OnuSn": serialNumber,
538 "Type": dhcpType,
539 "DstMac": dstMac,
540 "SrcMac": srcMac,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700541 }).Infof("Sent DHCP packet to the ONU")
542
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700543 dhcpLayer, _ := GetDhcpLayer(pkt)
544 dhcpMessageType, _ := GetDhcpMessageType(dhcpLayer)
545 if dhcpMessageType == layers.DHCPMsgTypeAck {
546 doneChannel <- true
547 }
548
549 } else {
550 // double tag the packet and send it to the NNI
Matteo Scandolo4a036262020-08-17 15:56:13 -0700551 // we don't really care about the tags as they are stripped before
552 // the packet is sent to the DHCP server
Matteo Scandolo8d281372020-09-03 16:23:37 -0700553 // in BBR we don't care about the pBit
554 doubleTaggedPkt, err := packetHandlers.PushDoubleTag(900, 900, pkt, 0)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700555 if err != nil {
Matteo Scandoloe0e9b772020-02-03 14:23:43 -0800556 dhcpLogger.WithFields(log.Fields{
557 "OnuId": onuId,
558 "IntfId": ponPortId,
559 "OnuSn": serialNumber,
560 "Type": dhcpType,
561 "error": err,
562 }).Error("Failed to add double tag to packet")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700563 }
564
565 pkt := openolt.UplinkPacket{
566 IntfId: 0, // BBSim does not care about which NNI, it has only one
567 Pkt: doubleTaggedPkt.Data(),
568 }
569 if _, err := client.UplinkPacketOut(context.Background(), &pkt); err != nil {
Matteo Scandoloe0e9b772020-02-03 14:23:43 -0800570 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700571 "OnuId": onuId,
572 "IntfId": ponPortId,
573 "OnuSn": serialNumber,
574 "Type": dhcpType,
575 "error": err,
576 }).Error("Failed to send DHCP packet out of the NNI Port")
577 }
Matteo Scandoloe0e9b772020-02-03 14:23:43 -0800578 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700579 "OnuId": onuId,
580 "IntfId": ponPortId,
581 "OnuSn": serialNumber,
582 "Type": dhcpType,
583 "DstMac": dstMac,
584 "SrcMac": srcMac,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700585 }).Infof("Sent DHCP packet out of the NNI Port")
586 }
587 return nil
588}