blob: 086093509e3f7ecb4f9a1d27b59821fd037b1ad9 [file] [log] [blame]
Matteo Scandolo47e69bb2019-08-28 15:41:12 -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 eapol
18
19import (
Matteo Scandolo40e067f2019-10-16 16:59:41 -070020 "context"
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070021 "crypto/md5"
22 "errors"
Shrey Baid688b4242020-07-10 20:40:10 +053023 "net"
24
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070025 "github.com/google/gopacket"
26 "github.com/google/gopacket/layers"
27 "github.com/looplab/fsm"
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070028 bbsim "github.com/opencord/bbsim/internal/bbsim/types"
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070029 omci "github.com/opencord/omci-sim"
Matteo Scandolo4f4ac792020-10-01 16:33:21 -070030 "github.com/opencord/voltha-protos/v4/go/openolt"
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070031 log "github.com/sirupsen/logrus"
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070032)
33
34var eapolLogger = log.WithFields(log.Fields{
35 "module": "EAPOL",
36})
37
38var eapolVersion uint8 = 1
Matteo Scandolo075b1892019-10-07 12:11:07 -070039var GetGemPortId = omci.GetGemPortId
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070040
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070041func sendEapolPktIn(msg bbsim.ByteMsg, portNo uint32, gemid uint32, stream bbsim.Stream) error {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070042 // FIXME unify sendDHCPPktIn and sendEapolPktIn methods
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070043
44 log.WithFields(log.Fields{
Shrey Baid688b4242020-07-10 20:40:10 +053045 "OnuId": msg.OnuId,
46 "IntfId": msg.IntfId,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070047 "GemPort": gemid,
Shrey Baid688b4242020-07-10 20:40:10 +053048 "Type": "EAPOL",
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070049 }).Trace("sending-pkt")
50
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070051 data := &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{
Matteo Scandolo27428702019-10-11 16:21:16 -070052 IntfType: "pon",
53 IntfId: msg.IntfId,
54 GemportId: uint32(gemid),
55 Pkt: msg.Bytes,
56 PortNo: portNo,
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070057 }}
58
59 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
60 eapolLogger.Errorf("Fail to send EAPOL PktInd indication. %v", err)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070061 return err
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070062 }
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070063 return nil
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070064}
65
66func getMD5Data(eap *layers.EAP) []byte {
67 i := byte(eap.Id)
68 C := []byte(eap.BaseLayer.Contents)[6:]
69 P := []byte{i, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64} // "password"
70 data := md5.Sum(append(P, C...))
71 ret := make([]byte, 16)
72 for j := 0; j < 16; j++ {
73 ret[j] = data[j]
74 }
75 return ret
76}
77
Matteo Scandolo40e067f2019-10-16 16:59:41 -070078func createEAPChallengeRequest(eapId uint8, payload []byte) *layers.EAP {
79 eap := layers.EAP{
80 Code: layers.EAPCodeRequest,
81 Id: eapId,
82 Length: 22,
83 Type: layers.EAPTypeOTP,
84 TypeData: payload,
85 }
86 return &eap
87}
88
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070089func createEAPChallengeResponse(eapId uint8, payload []byte) *layers.EAP {
90 eap := layers.EAP{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070091 Code: layers.EAPCodeResponse,
92 Id: eapId,
93 Length: 22,
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070094 Type: layers.EAPTypeOTP,
95 TypeData: payload,
96 }
97 return &eap
98}
99
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700100func createEAPIdentityRequest(eapId uint8) *layers.EAP {
101 eap := layers.EAP{Code: layers.EAPCodeRequest,
102 Id: eapId,
103 Length: 9,
104 Type: layers.EAPTypeIdentity,
105 TypeData: []byte{0x75, 0x73, 0x65, 0x72}}
106 return &eap
107}
108
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700109func createEAPIdentityResponse(eapId uint8) *layers.EAP {
110 eap := layers.EAP{Code: layers.EAPCodeResponse,
111 Id: eapId,
112 Length: 9,
113 Type: layers.EAPTypeIdentity,
114 TypeData: []byte{0x75, 0x73, 0x65, 0x72}}
115 return &eap
116}
117
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700118func createEAPSuccess(eapId uint8) *layers.EAP {
119 eap := layers.EAP{
120 Code: layers.EAPCodeSuccess,
121 Id: eapId,
122 Length: 9,
123 Type: layers.EAPTypeNone,
124 TypeData: []byte{0x75, 0x73, 0x65, 0x72}}
125 return &eap
126}
127
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700128func createEAPOLPkt(eap *layers.EAP, onuId uint32, intfId uint32) []byte {
129 buffer := gopacket.NewSerializeBuffer()
130 options := gopacket.SerializeOptions{}
131
132 ethernetLayer := &layers.Ethernet{
Matteo Scandolo4a036262020-08-17 15:56:13 -0700133 SrcMAC: net.HardwareAddr{0x2e, 0x60, byte(0), byte(intfId), byte(onuId), byte(0)},
134 DstMAC: net.HardwareAddr{0x2e, 0x60, byte(0), byte(intfId), byte(onuId), byte(0)},
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700135 EthernetType: layers.EthernetTypeEAPOL,
136 }
137
Shrey Baid688b4242020-07-10 20:40:10 +0530138 _ = gopacket.SerializeLayers(buffer, options,
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700139 ethernetLayer,
140 &layers.EAPOL{Version: eapolVersion, Type: 0, Length: eap.Length},
141 eap,
142 )
143
144 bytes := buffer.Bytes()
145 return bytes
146}
147
148func extractEAP(pkt gopacket.Packet) (*layers.EAP, error) {
149 layerEAP := pkt.Layer(layers.LayerTypeEAP)
150 eap, _ := layerEAP.(*layers.EAP)
151 if eap == nil {
152 return nil, errors.New("Cannot extract EAP")
153 }
154 return eap, nil
155}
156
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700157func extractEAPOL(pkt gopacket.Packet) (*layers.EAPOL, error) {
158 layerEAPOL := pkt.Layer(layers.LayerTypeEAPOL)
159 eap, _ := layerEAPOL.(*layers.EAPOL)
160 if eap == nil {
161 return nil, errors.New("Cannot extract EAPOL")
162 }
163 return eap, nil
164}
165
166func sendEapolPktOut(client openolt.OpenoltClient, intfId uint32, onuId uint32, pkt []byte) error {
167 onuPacket := openolt.OnuPacket{
168 IntfId: intfId,
169 OnuId: onuId,
170 PortNo: onuId,
171 GemportId: 1,
172 Pkt: pkt,
173 }
174
175 if _, err := client.OnuPacketOut(context.Background(), &onuPacket); err != nil {
176 return err
177 }
178 return nil
179}
180
Matteo Scandolo075b1892019-10-07 12:11:07 -0700181func updateAuthFailed(onuId uint32, ponPortId uint32, serialNumber string, onuStateMachine *fsm.FSM) error {
182 if err := onuStateMachine.Event("auth_failed"); err != nil {
183 eapolLogger.WithFields(log.Fields{
184 "OnuId": onuId,
185 "IntfId": ponPortId,
186 "OnuSn": serialNumber,
187 }).Errorf("Error while transitioning ONU State %v", err)
188 return err
189 }
190 return nil
191}
192
Matteo Scandolo4a036262020-08-17 15:56:13 -0700193func SendEapStart(onuId uint32, ponPortId uint32, serialNumber string, portNo uint32, macAddress net.HardwareAddr, gemPort uint32, stateMachine *fsm.FSM, stream bbsim.Stream) error {
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700194
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700195 // TODO use createEAPOLPkt
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700196 buffer := gopacket.NewSerializeBuffer()
197 options := gopacket.SerializeOptions{}
198
199 ethernetLayer := &layers.Ethernet{
Matteo Scandolo86e8ce62019-10-11 12:03:10 -0700200 SrcMAC: macAddress,
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700201 DstMAC: net.HardwareAddr{0x01, 0x80, 0xC2, 0x00, 0x00, 0x03},
202 EthernetType: layers.EthernetTypeEAPOL,
203 }
204
Shrey Baid688b4242020-07-10 20:40:10 +0530205 _ = gopacket.SerializeLayers(buffer, options,
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700206 ethernetLayer,
207 &layers.EAPOL{Version: eapolVersion, Type: 1, Length: 0},
208 )
209
210 msg := buffer.Bytes()
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700211 // TODO end createEAPOLPkt
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700212
213 data := &openolt.Indication_PktInd{
214 PktInd: &openolt.PacketIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700215 IntfType: "pon",
216 IntfId: ponPortId,
Matteo Scandolo4a036262020-08-17 15:56:13 -0700217 GemportId: gemPort,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700218 Pkt: msg,
Matteo Scandolo27428702019-10-11 16:21:16 -0700219 PortNo: portNo,
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700220 },
221 }
Matteo Scandolo075b1892019-10-07 12:11:07 -0700222
Matteo Scandolo4a036262020-08-17 15:56:13 -0700223 err := stream.Send(&openolt.Indication{Data: data})
Matteo Scandolo075b1892019-10-07 12:11:07 -0700224 if err != nil {
225 eapolLogger.WithFields(log.Fields{
226 "OnuId": onuId,
227 "IntfId": ponPortId,
228 "OnuSn": serialNumber,
229 }).Errorf("Can't send EapStart Message: %s", err)
230
Matteo Scandolo4a036262020-08-17 15:56:13 -0700231 if err := updateAuthFailed(onuId, ponPortId, serialNumber, stateMachine); err != nil {
Matteo Scandolo075b1892019-10-07 12:11:07 -0700232 return err
233 }
234 return err
235 }
236
Matteo Scandolo27428702019-10-11 16:21:16 -0700237 eapolLogger.WithFields(log.Fields{
238 "OnuId": onuId,
239 "IntfId": ponPortId,
240 "OnuSn": serialNumber,
241 "PortNo": portNo,
242 }).Debugf("Sent EapStart packet")
Matteo Scandolo075b1892019-10-07 12:11:07 -0700243
Matteo Scandolo4a036262020-08-17 15:56:13 -0700244 if err := stateMachine.Event("eap_start_sent"); err != nil {
Matteo Scandolo075b1892019-10-07 12:11:07 -0700245 eapolLogger.WithFields(log.Fields{
246 "OnuId": onuId,
247 "IntfId": ponPortId,
248 "OnuSn": serialNumber,
249 }).Errorf("Error while transitioning ONU State %v", err)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700250 return err
251 }
252 return nil
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700253}
Matteo Scandolo075b1892019-10-07 12:11:07 -0700254
Matteo Scandolo4a036262020-08-17 15:56:13 -0700255func HandleNextPacket(onuId uint32, ponPortId uint32, gemPortId uint32, serialNumber string, portNo uint32, stateMachine *fsm.FSM, pkt gopacket.Packet, stream bbsim.Stream, client openolt.OpenoltClient) {
Matteo Scandolo27428702019-10-11 16:21:16 -0700256
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700257 eap, eapErr := extractEAP(pkt)
258
259 eapol, eapolErr := extractEAPOL(pkt)
260
261 if eapErr != nil && eapolErr != nil {
262 log.Fatalf("Failed to Extract EAP: %v - %v", eapErr, eapolErr)
263 return
Matteo Scandolo075b1892019-10-07 12:11:07 -0700264 }
265
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700266 fields := log.Fields{}
267 if eap != nil {
268 fields = log.Fields{
269 "Code": eap.Code,
270 "Type": eap.Type,
271 "OnuId": onuId,
272 "IntfId": ponPortId,
273 "OnuSn": serialNumber,
274 }
275 } else if eapol != nil {
276 fields = log.Fields{
277 "Type": eapol.Type,
278 "OnuId": onuId,
279 "IntfId": ponPortId,
280 "OnuSn": serialNumber,
281 }
282 }
Matteo Scandolo27428702019-10-11 16:21:16 -0700283
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700284 log.WithFields(fields).Tracef("Handle Next EAPOL Packet")
285
286 if eapol != nil && eapol.Type == layers.EAPOLTypeStart {
287 identityRequest := createEAPIdentityRequest(1)
288 pkt := createEAPOLPkt(identityRequest, onuId, ponPortId)
289
290 if err := sendEapolPktOut(client, ponPortId, onuId, pkt); err != nil {
291 log.WithFields(log.Fields{
292 "OnuId": onuId,
293 "IntfId": ponPortId,
294 "OnuSn": serialNumber,
295 "error": err,
296 }).Errorf("Error while sending EAPIdentityRequest packet")
297 return
298 }
299
300 log.WithFields(log.Fields{
301 "OnuId": onuId,
302 "IntfId": ponPortId,
303 "OnuSn": serialNumber,
304 }).Infof("Sent EAPIdentityRequest packet")
305 return
306 } else if eap.Code == layers.EAPCodeRequest && eap.Type == layers.EAPTypeIdentity {
Matteo Scandolo075b1892019-10-07 12:11:07 -0700307 reseap := createEAPIdentityResponse(eap.Id)
308 pkt := createEAPOLPkt(reseap, onuId, ponPortId)
309
310 msg := bbsim.ByteMsg{
311 IntfId: ponPortId,
312 OnuId: onuId,
313 Bytes: pkt,
314 }
315
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700316 if err := sendEapolPktIn(msg, portNo, gemPortId, stream); err != nil {
317 _ = stateMachine.Event("auth_failed")
318 return
319 }
Matteo Scandolo075b1892019-10-07 12:11:07 -0700320 eapolLogger.WithFields(log.Fields{
321 "OnuId": onuId,
322 "IntfId": ponPortId,
323 "OnuSn": serialNumber,
Matteo Scandolo27428702019-10-11 16:21:16 -0700324 "PortNo": portNo,
325 }).Debugf("Sent EAPIdentityResponse packet")
Matteo Scandolo4a036262020-08-17 15:56:13 -0700326 if err := stateMachine.Event("eap_response_identity_sent"); err != nil {
Matteo Scandolo075b1892019-10-07 12:11:07 -0700327 eapolLogger.WithFields(log.Fields{
328 "OnuId": onuId,
329 "IntfId": ponPortId,
330 "OnuSn": serialNumber,
331 }).Errorf("Error while transitioning ONU State %v", err)
332 }
333
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700334 } else if eap.Code == layers.EAPCodeResponse && eap.Type == layers.EAPTypeIdentity {
335 senddata := getMD5Data(eap)
336 senddata = append([]byte{0x10}, senddata...)
337 challengeRequest := createEAPChallengeRequest(eap.Id, senddata)
338 pkt := createEAPOLPkt(challengeRequest, onuId, ponPortId)
339
340 if err := sendEapolPktOut(client, ponPortId, onuId, pkt); err != nil {
341 log.WithFields(log.Fields{
342 "OnuId": onuId,
343 "IntfId": ponPortId,
344 "OnuSn": serialNumber,
345 "error": err,
346 }).Errorf("Error while sending EAPChallengeRequest packet")
347 return
348 }
349 log.WithFields(log.Fields{
350 "OnuId": onuId,
351 "IntfId": ponPortId,
352 "OnuSn": serialNumber,
353 }).Infof("Sent EAPChallengeRequest packet")
354 return
Matteo Scandolo075b1892019-10-07 12:11:07 -0700355 } else if eap.Code == layers.EAPCodeRequest && eap.Type == layers.EAPTypeOTP {
356 senddata := getMD5Data(eap)
357 senddata = append([]byte{0x10}, senddata...)
358 sendeap := createEAPChallengeResponse(eap.Id, senddata)
359 pkt := createEAPOLPkt(sendeap, onuId, ponPortId)
360
361 msg := bbsim.ByteMsg{
362 IntfId: ponPortId,
363 OnuId: onuId,
364 Bytes: pkt,
365 }
366
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700367 if err := sendEapolPktIn(msg, portNo, gemPortId, stream); err != nil {
368 _ = stateMachine.Event("auth_failed")
369 return
370 }
Matteo Scandolo075b1892019-10-07 12:11:07 -0700371 eapolLogger.WithFields(log.Fields{
372 "OnuId": onuId,
373 "IntfId": ponPortId,
374 "OnuSn": serialNumber,
Matteo Scandolo27428702019-10-11 16:21:16 -0700375 "PortNo": portNo,
376 }).Debugf("Sent EAPChallengeResponse packet")
Matteo Scandolo4a036262020-08-17 15:56:13 -0700377 if err := stateMachine.Event("eap_response_challenge_sent"); err != nil {
Matteo Scandolo075b1892019-10-07 12:11:07 -0700378 eapolLogger.WithFields(log.Fields{
379 "OnuId": onuId,
380 "IntfId": ponPortId,
381 "OnuSn": serialNumber,
382 }).Errorf("Error while transitioning ONU State %v", err)
383 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700384 } else if eap.Code == layers.EAPCodeResponse && eap.Type == layers.EAPTypeOTP {
385 eapSuccess := createEAPSuccess(eap.Id)
386 pkt := createEAPOLPkt(eapSuccess, onuId, ponPortId)
387
388 if err := sendEapolPktOut(client, ponPortId, onuId, pkt); err != nil {
389 log.WithFields(log.Fields{
390 "OnuId": onuId,
391 "IntfId": ponPortId,
392 "OnuSn": serialNumber,
393 "error": err,
394 }).Errorf("Error while sending EAPSuccess packet")
395 return
396 }
397
398 log.WithFields(log.Fields{
399 "OnuId": onuId,
400 "IntfId": ponPortId,
401 "OnuSn": serialNumber,
402 }).Infof("Sent EAP Success packet")
403
Matteo Scandolo4a036262020-08-17 15:56:13 -0700404 if err := stateMachine.Event("send_dhcp_flow"); err != nil {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700405 eapolLogger.WithFields(log.Fields{
406 "OnuId": onuId,
407 "IntfId": ponPortId,
408 "OnuSn": serialNumber,
409 }).Errorf("Error while transitioning ONU State %v", err)
410 }
Matteo Scandolo075b1892019-10-07 12:11:07 -0700411 } else if eap.Code == layers.EAPCodeSuccess && eap.Type == layers.EAPTypeNone {
412 eapolLogger.WithFields(log.Fields{
413 "OnuId": onuId,
414 "IntfId": ponPortId,
415 "OnuSn": serialNumber,
Matteo Scandolo27428702019-10-11 16:21:16 -0700416 "PortNo": portNo,
417 }).Debugf("Received EAPSuccess packet")
Matteo Scandolo4a036262020-08-17 15:56:13 -0700418 if err := stateMachine.Event("eap_response_success_received"); err != nil {
Matteo Scandolo075b1892019-10-07 12:11:07 -0700419 eapolLogger.WithFields(log.Fields{
420 "OnuId": onuId,
421 "IntfId": ponPortId,
422 "OnuSn": serialNumber,
423 }).Errorf("Error while transitioning ONU State %v", err)
424 }
425 eapolLogger.WithFields(log.Fields{
426 "OnuId": onuId,
427 "IntfId": ponPortId,
428 "OnuSn": serialNumber,
429 }).Infof("EAPOL State machine completed")
430 return
431 }
432}