blob: 20186ea67152cc21da2803836ab3ea12f4b09791 [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 Scandolo90d08f62020-10-29 12:06:55 -0700161func serializeDHCPPacket(cTag int, srcMac net.HardwareAddr, dhcp *layers.DHCPv4, pbit uint8) (gopacket.Packet, 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
Matteo Scandolo90d08f62020-10-29 12:06:55 -0700203 return taggedPkt, 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 Scandolo8a574812021-05-20 15:18:53 -0700261 // TODO the adapter uses Onu, Uni and gemPort to route the packet,
262 // stop using PortNo to ensure consistent behavior
263 // requires voltha-protos:4.1.6
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700264 data := &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{
Matteo Scandolo27428702019-10-11 16:21:16 -0700265 IntfType: "pon",
266 IntfId: msg.IntfId,
Matteo Scandolo4a036262020-08-17 15:56:13 -0700267 GemportId: gemPortId,
Matteo Scandolo27428702019-10-11 16:21:16 -0700268 Pkt: msg.Bytes,
269 PortNo: portNo,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700270 }}
271
272 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
273 dhcpLogger.Errorf("Fail to send DHCP PktInd indication. %v", err)
274 return err
275 }
276 return nil
277}
278
Matteo Scandolo24a88c42020-09-17 14:55:28 -0700279func sendDHCPRequest(ponPortId uint32, onuId uint32, serviceName string, serialNumber string, portNo uint32,
Matteo Scandolo8d281372020-09-03 16:23:37 -0700280 cTag int, gemPortId uint32, onuStateMachine *fsm.FSM, onuHwAddress net.HardwareAddr,
281 offeredIp net.IP, pbit uint8, stream bbsim.Stream) error {
Matteo Scandolobfcfbb32020-09-28 14:14:39 -0700282 dhcp := createDHCPReq(ponPortId, onuId, onuHwAddress, offeredIp, gemPortId)
Matteo Scandolo90d08f62020-10-29 12:06:55 -0700283 pkt, err := serializeDHCPPacket(cTag, onuHwAddress, dhcp, pbit)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700284
285 if err != nil {
Matteo Scandolofe9ac252019-10-25 11:40:17 -0700286 dhcpLogger.WithFields(log.Fields{
287 "OnuId": onuId,
288 "IntfId": ponPortId,
289 "OnuSn": serialNumber,
290 "OfferedIp": offeredIp.String(),
291 }).Errorf("Cannot serializeDHCPPacket: %s", err)
292 if err := updateDhcpFailed(onuId, ponPortId, serialNumber, onuStateMachine); err != nil {
293 return err
294 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700295 return err
296 }
297 // NOTE I don't think we need to tag the packet
298 //taggedPkt, err := packetHandlers.PushSingleTag(cTag, pkt)
299
300 msg := bbsim.ByteMsg{
301 IntfId: ponPortId,
302 OnuId: onuId,
Matteo Scandolo90d08f62020-10-29 12:06:55 -0700303 Bytes: pkt.Data(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700304 }
305
Matteo Scandolo4a036262020-08-17 15:56:13 -0700306 if err := sendDHCPPktIn(msg, portNo, gemPortId, stream); err != nil {
Matteo Scandolofe9ac252019-10-25 11:40:17 -0700307 dhcpLogger.WithFields(log.Fields{
308 "OnuId": onuId,
309 "IntfId": ponPortId,
310 "OnuSn": serialNumber,
311 }).Errorf("Cannot sendDHCPPktIn: %s", err)
312 if err := updateDhcpFailed(onuId, ponPortId, serialNumber, onuStateMachine); err != nil {
313 return err
314 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700315 return err
316 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700317
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700318 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700319 "OnuId": onuId,
320 "IntfId": ponPortId,
321 "OnuSn": serialNumber,
322 "OfferedIp": offeredIp.String(),
323 "ServiceName": serviceName,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700324 }).Infof("DHCPRequest Sent")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700325 return nil
326}
327
Matteo Scandolo075b1892019-10-07 12:11:07 -0700328func updateDhcpFailed(onuId uint32, ponPortId uint32, serialNumber string, onuStateMachine *fsm.FSM) error {
329 if err := onuStateMachine.Event("dhcp_failed"); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700330 dhcpLogger.WithFields(log.Fields{
331 "OnuId": onuId,
332 "IntfId": ponPortId,
333 "OnuSn": serialNumber,
Matteo Scandolo075b1892019-10-07 12:11:07 -0700334 }).Errorf("Error while transitioning ONU State %v", err)
335 return err
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700336 }
Matteo Scandolo075b1892019-10-07 12:11:07 -0700337 return nil
338}
339
Matteo Scandolo24a88c42020-09-17 14:55:28 -0700340func SendDHCPDiscovery(ponPortId uint32, onuId uint32, serviceName string, cTag int, gemPortId uint32,
Matteo Scandolo8a574812021-05-20 15:18:53 -0700341 serialNumber string, portNo uint32, uniId uint32, stateMachine *fsm.FSM, onuHwAddress net.HardwareAddr,
Matteo Scandolo8d281372020-09-03 16:23:37 -0700342 pbit uint8, stream bbsim.Stream) error {
343
Matteo Scandolo24a88c42020-09-17 14:55:28 -0700344 dhcp := createDHCPDisc(ponPortId, onuId, gemPortId, onuHwAddress)
Matteo Scandolo90d08f62020-10-29 12:06:55 -0700345 pkt, err := serializeDHCPPacket(cTag, onuHwAddress, dhcp, pbit)
Matteo Scandoloe0e9b772020-02-03 14:23:43 -0800346
Matteo Scandolo075b1892019-10-07 12:11:07 -0700347 if err != nil {
Matteo Scandolofe9ac252019-10-25 11:40:17 -0700348 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700349 "OnuId": onuId,
350 "IntfId": ponPortId,
351 "OnuSn": serialNumber,
Matteo Scandolo8a574812021-05-20 15:18:53 -0700352 "PortNo": portNo,
353 "UniId": uniId,
354 "GemPortId": gemPortId,
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700355 "ServiceName": serviceName,
Matteo Scandolofe9ac252019-10-25 11:40:17 -0700356 }).Errorf("Cannot serializeDHCPPacket: %s", err)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700357 if err := updateDhcpFailed(onuId, ponPortId, serialNumber, stateMachine); err != nil {
Matteo Scandolofe9ac252019-10-25 11:40:17 -0700358 return err
359 }
Matteo Scandolo075b1892019-10-07 12:11:07 -0700360 return err
361 }
362 // NOTE I don't think we need to tag the packet
363 //taggedPkt, err := packetHandlers.PushSingleTag(cTag, pkt)
364
365 msg := bbsim.ByteMsg{
366 IntfId: ponPortId,
367 OnuId: onuId,
Matteo Scandolo90d08f62020-10-29 12:06:55 -0700368 Bytes: pkt.Data(),
Matteo Scandolo075b1892019-10-07 12:11:07 -0700369 }
370
Matteo Scandolo4a036262020-08-17 15:56:13 -0700371 if err := sendDHCPPktIn(msg, portNo, gemPortId, stream); err != nil {
Matteo Scandolofe9ac252019-10-25 11:40:17 -0700372 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700373 "OnuId": onuId,
374 "IntfId": ponPortId,
375 "OnuSn": serialNumber,
Matteo Scandolo8a574812021-05-20 15:18:53 -0700376 "PortNo": portNo,
377 "UniId": uniId,
378 "GemPortId": gemPortId,
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700379 "ServiceName": serviceName,
Matteo Scandolofe9ac252019-10-25 11:40:17 -0700380 }).Errorf("Cannot sendDHCPPktIn: %s", err)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700381 if err := updateDhcpFailed(onuId, ponPortId, serialNumber, stateMachine); err != nil {
Matteo Scandolo075b1892019-10-07 12:11:07 -0700382 return err
383 }
384 return err
385 }
386 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700387 "OnuId": onuId,
388 "IntfId": ponPortId,
389 "OnuSn": serialNumber,
Matteo Scandolo8a574812021-05-20 15:18:53 -0700390 "PortNo": portNo,
391 "UniId": uniId,
392 "GemPortId": gemPortId,
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700393 "ServiceName": serviceName,
394 }).Info("DHCPDiscovery Sent")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700395
Matteo Scandolo4a036262020-08-17 15:56:13 -0700396 if err := stateMachine.Event("dhcp_discovery_sent"); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700397 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700398 "OnuId": onuId,
399 "IntfId": ponPortId,
400 "OnuSn": serialNumber,
Matteo Scandolo8a574812021-05-20 15:18:53 -0700401 "PortNo": portNo,
402 "UniId": uniId,
403 "GemPortId": gemPortId,
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700404 "ServiceName": serviceName,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700405 }).Errorf("Error while transitioning ONU State %v", err)
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700406 return err
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700407 }
Matteo Scandolo075b1892019-10-07 12:11:07 -0700408 return nil
409}
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700410
Matteo Scandolo24a88c42020-09-17 14:55:28 -0700411func HandleNextPacket(onuId uint32, ponPortId uint32, serviceName string, serialNumber string, portNo uint32,
Matteo Scandolo8a574812021-05-20 15:18:53 -0700412 cTag int, gemPortId uint32, uniId uint32, onuHwAddress net.HardwareAddr, onuStateMachine *fsm.FSM,
Matteo Scandolo8d281372020-09-03 16:23:37 -0700413 pkt gopacket.Packet, pbit uint8, stream bbsim.Stream) error {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700414
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700415 dhcpLayer, err := GetDhcpLayer(pkt)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700416 if err != nil {
417 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700418 "OnuId": onuId,
419 "IntfId": ponPortId,
420 "OnuSn": serialNumber,
Matteo Scandolo8a574812021-05-20 15:18:53 -0700421 "PortNo": portNo,
422 "UniId": uniId,
423 "GemPortId": gemPortId,
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700424 "ServiceName": serviceName,
Matteo Scandolo075b1892019-10-07 12:11:07 -0700425 }).Errorf("Can't get DHCP Layer from Packet: %v", err)
426 if err := updateDhcpFailed(onuId, ponPortId, serialNumber, onuStateMachine); err != nil {
427 return err
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700428 }
Matteo Scandolo075b1892019-10-07 12:11:07 -0700429 return err
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700430 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700431 dhcpMessageType, err := GetDhcpMessageType(dhcpLayer)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700432 if err != nil {
433 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700434 "OnuId": onuId,
435 "IntfId": ponPortId,
436 "OnuSn": serialNumber,
Matteo Scandolo8a574812021-05-20 15:18:53 -0700437 "PortNo": portNo,
438 "UniId": uniId,
439 "GemPortId": gemPortId,
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700440 "ServiceName": serviceName,
Matteo Scandolo075b1892019-10-07 12:11:07 -0700441 }).Errorf("Can't get DHCP Message Type from DHCP Layer: %v", err)
442 if err := updateDhcpFailed(onuId, ponPortId, serialNumber, onuStateMachine); err != nil {
443 return err
444 }
445 return err
446 }
447
448 if dhcpLayer.Operation == layers.DHCPOpReply {
449 if dhcpMessageType == layers.DHCPMsgTypeOffer {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700450 offeredIp := dhcpLayer.YourClientIP
Matteo Scandolo24a88c42020-09-17 14:55:28 -0700451 if err := sendDHCPRequest(ponPortId, onuId, serviceName, serialNumber, portNo, cTag, gemPortId, onuStateMachine, onuHwAddress, offeredIp, pbit, stream); err != nil {
Matteo Scandolo075b1892019-10-07 12:11:07 -0700452 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700453 "OnuId": onuId,
454 "IntfId": ponPortId,
455 "OnuSn": serialNumber,
Matteo Scandolo8a574812021-05-20 15:18:53 -0700456 "PortNo": portNo,
457 "UniId": uniId,
458 "GemPortId": gemPortId,
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700459 "ServiceName": serviceName,
Matteo Scandolo075b1892019-10-07 12:11:07 -0700460 }).Errorf("Can't send DHCP Request: %s", err)
461 if err := updateDhcpFailed(onuId, ponPortId, serialNumber, onuStateMachine); err != nil {
462 return err
463 }
464 return err
465 }
466 if err := onuStateMachine.Event("dhcp_request_sent"); err != nil {
467 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700468 "OnuId": onuId,
469 "IntfId": ponPortId,
470 "OnuSn": serialNumber,
Matteo Scandolo8a574812021-05-20 15:18:53 -0700471 "PortNo": portNo,
472 "UniId": uniId,
473 "GemPortId": gemPortId,
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700474 "ServiceName": serviceName,
475 }).Errorf("Error while transitioning State %v", err)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700476 }
477
478 } else if dhcpMessageType == layers.DHCPMsgTypeAck {
479 // NOTE once the ack is received we don't need to do anything but change the state
480 if err := onuStateMachine.Event("dhcp_ack_received"); err != nil {
481 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700482 "OnuId": onuId,
483 "IntfId": ponPortId,
484 "OnuSn": serialNumber,
Matteo Scandolo8a574812021-05-20 15:18:53 -0700485 "PortNo": portNo,
486 "UniId": uniId,
487 "GemPortId": gemPortId,
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700488 "ServiceName": serviceName,
489 }).Errorf("Error while transitioning State %v", err)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700490 }
491 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700492 "OnuId": onuId,
493 "IntfId": ponPortId,
494 "OnuSn": serialNumber,
Matteo Scandolo8a574812021-05-20 15:18:53 -0700495 "PortNo": portNo,
496 "UniId": uniId,
497 "GemPortId": gemPortId,
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700498 "ServiceName": serviceName,
Matteo Scandolo075b1892019-10-07 12:11:07 -0700499 }).Infof("DHCP State machine completed")
500 }
501 // NOTE do we need to care about DHCPMsgTypeRelease??
502 } else {
503 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700504 "OnuId": onuId,
505 "IntfId": ponPortId,
506 "OnuSn": serialNumber,
Matteo Scandolo8a574812021-05-20 15:18:53 -0700507 "PortNo": portNo,
508 "UniId": uniId,
509 "GemPortId": gemPortId,
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700510 "ServiceName": serviceName,
Matteo Scandolo075b1892019-10-07 12:11:07 -0700511 }).Warnf("Unsupported DHCP Operation: %s", dhcpLayer.Operation.String())
512 }
Matteo Scandolofe9ac252019-10-25 11:40:17 -0700513
Matteo Scandolo075b1892019-10-07 12:11:07 -0700514 return nil
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700515}
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700516
517// This method handle the BBR DHCP Packets
518// BBR does not need to do anything but forward the packets in the correct direction
Matteo Scandolo4a036262020-08-17 15:56:13 -0700519func HandleNextBbrPacket(onuId uint32, ponPortId uint32, serialNumber string, doneChannel chan bool, pkt gopacket.Packet, client openolt.OpenoltClient) error {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700520
521 // check if the packet is going:
522 // - outgouing: toward the DHCP
523 // - incoming: toward the ONU
524 isIncoming := packetHandlers.IsIncomingPacket(pkt)
Matteo Scandoloe0e9b772020-02-03 14:23:43 -0800525 dhcpLogger.Tracef("Is Incoming: %t", isIncoming)
526
527 pkt, err := packetHandlers.PopSingleTag(pkt)
528 if err != nil {
529 dhcpLogger.WithFields(log.Fields{
530 "OnuId": onuId,
531 "IntfId": ponPortId,
532 "OnuSn": serialNumber,
533 "error": err,
534 }).Fatalf("Can't untag packet")
535 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700536
537 dhcpType, err := GetDhcpPacketType(pkt)
538 if err != nil {
Matteo Scandoloe0e9b772020-02-03 14:23:43 -0800539 dhcpLogger.WithFields(log.Fields{
540 "OnuId": onuId,
541 "IntfId": ponPortId,
542 "OnuSn": serialNumber,
543 "error": err,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700544 }).Fatalf("Can't find DHCP type for packet")
545 }
546
547 srcMac, _ := packetHandlers.GetSrcMacAddressFromPacket(pkt)
548 dstMac, _ := packetHandlers.GetDstMacAddressFromPacket(pkt)
549
Shrey Baid688b4242020-07-10 20:40:10 +0530550 if isIncoming {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700551
552 onuPacket := openolt.OnuPacket{
553 IntfId: ponPortId,
554 OnuId: onuId,
555 PortNo: onuId,
556 GemportId: 1,
557 Pkt: pkt.Data(),
558 }
559
560 if _, err := client.OnuPacketOut(context.Background(), &onuPacket); err != nil {
Matteo Scandoloe0e9b772020-02-03 14:23:43 -0800561 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700562 "OnuId": onuId,
563 "IntfId": ponPortId,
564 "OnuSn": serialNumber,
565 "Type": dhcpType,
566 "error": err,
567 }).Error("Failed to send DHCP packet to the ONU")
568 }
569
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 "DstMac": dstMac,
576 "SrcMac": srcMac,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700577 }).Infof("Sent DHCP packet to the ONU")
578
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700579 dhcpLayer, _ := GetDhcpLayer(pkt)
580 dhcpMessageType, _ := GetDhcpMessageType(dhcpLayer)
581 if dhcpMessageType == layers.DHCPMsgTypeAck {
582 doneChannel <- true
583 }
584
585 } else {
586 // double tag the packet and send it to the NNI
Matteo Scandolo4a036262020-08-17 15:56:13 -0700587 // we don't really care about the tags as they are stripped before
588 // the packet is sent to the DHCP server
Matteo Scandolo8d281372020-09-03 16:23:37 -0700589 // in BBR we don't care about the pBit
590 doubleTaggedPkt, err := packetHandlers.PushDoubleTag(900, 900, pkt, 0)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700591 if err != nil {
Matteo Scandoloe0e9b772020-02-03 14:23:43 -0800592 dhcpLogger.WithFields(log.Fields{
593 "OnuId": onuId,
594 "IntfId": ponPortId,
595 "OnuSn": serialNumber,
596 "Type": dhcpType,
597 "error": err,
598 }).Error("Failed to add double tag to packet")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700599 }
600
601 pkt := openolt.UplinkPacket{
602 IntfId: 0, // BBSim does not care about which NNI, it has only one
603 Pkt: doubleTaggedPkt.Data(),
604 }
605 if _, err := client.UplinkPacketOut(context.Background(), &pkt); err != nil {
Matteo Scandoloe0e9b772020-02-03 14:23:43 -0800606 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700607 "OnuId": onuId,
608 "IntfId": ponPortId,
609 "OnuSn": serialNumber,
610 "Type": dhcpType,
611 "error": err,
612 }).Error("Failed to send DHCP packet out of the NNI Port")
Matteo Scandolo90d08f62020-10-29 12:06:55 -0700613 return err
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700614 }
Matteo Scandoloe0e9b772020-02-03 14:23:43 -0800615 dhcpLogger.WithFields(log.Fields{
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700616 "OnuId": onuId,
617 "IntfId": ponPortId,
618 "OnuSn": serialNumber,
619 "Type": dhcpType,
620 "DstMac": dstMac,
621 "SrcMac": srcMac,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700622 }).Infof("Sent DHCP packet out of the NNI Port")
623 }
624 return nil
625}