blob: f31a068ba2c92fd23907c4804849cdf6abe88ce2 [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 (
20 "crypto/md5"
21 "errors"
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070022 "github.com/google/gopacket"
23 "github.com/google/gopacket/layers"
24 "github.com/looplab/fsm"
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070025 bbsim "github.com/opencord/bbsim/internal/bbsim/types"
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070026 omci "github.com/opencord/omci-sim"
27 "github.com/opencord/voltha-protos/go/openolt"
28 log "github.com/sirupsen/logrus"
29 "net"
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070030)
31
32var eapolLogger = log.WithFields(log.Fields{
33 "module": "EAPOL",
34})
35
36var eapolVersion uint8 = 1
Matteo Scandolo075b1892019-10-07 12:11:07 -070037var GetGemPortId = omci.GetGemPortId
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070038
Matteo Scandolo27428702019-10-11 16:21:16 -070039func sendEapolPktIn(msg bbsim.ByteMsg, portNo uint32, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070040 // FIXME unify sendDHCPPktIn and sendEapolPktIn methods
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070041 gemid, err := omci.GetGemPortId(msg.IntfId, msg.OnuId)
42 if err != nil {
43 eapolLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070044 "OnuId": msg.OnuId,
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070045 "IntfId": msg.IntfId,
46 }).Errorf("Can't retrieve GemPortId: %s", err)
47 return
48 }
49 data := &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{
Matteo Scandolo27428702019-10-11 16:21:16 -070050 IntfType: "pon",
51 IntfId: msg.IntfId,
52 GemportId: uint32(gemid),
53 Pkt: msg.Bytes,
54 PortNo: portNo,
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070055 }}
56
57 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
58 eapolLogger.Errorf("Fail to send EAPOL PktInd indication. %v", err)
59 return
60 }
61}
62
63func getMD5Data(eap *layers.EAP) []byte {
64 i := byte(eap.Id)
65 C := []byte(eap.BaseLayer.Contents)[6:]
66 P := []byte{i, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64} // "password"
67 data := md5.Sum(append(P, C...))
68 ret := make([]byte, 16)
69 for j := 0; j < 16; j++ {
70 ret[j] = data[j]
71 }
72 return ret
73}
74
75func createEAPChallengeResponse(eapId uint8, payload []byte) *layers.EAP {
76 eap := layers.EAP{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070077 Code: layers.EAPCodeResponse,
78 Id: eapId,
79 Length: 22,
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070080 Type: layers.EAPTypeOTP,
81 TypeData: payload,
82 }
83 return &eap
84}
85
86func createEAPIdentityResponse(eapId uint8) *layers.EAP {
87 eap := layers.EAP{Code: layers.EAPCodeResponse,
88 Id: eapId,
89 Length: 9,
90 Type: layers.EAPTypeIdentity,
91 TypeData: []byte{0x75, 0x73, 0x65, 0x72}}
92 return &eap
93}
94
95func createEAPOLPkt(eap *layers.EAP, onuId uint32, intfId uint32) []byte {
96 buffer := gopacket.NewSerializeBuffer()
97 options := gopacket.SerializeOptions{}
98
99 ethernetLayer := &layers.Ethernet{
100 SrcMAC: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(intfId), byte(onuId)},
101 DstMAC: net.HardwareAddr{0x01, 0x80, 0xC2, 0x00, 0x00, 0x03},
102 EthernetType: layers.EthernetTypeEAPOL,
103 }
104
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700105 gopacket.SerializeLayers(buffer, options,
106 ethernetLayer,
107 &layers.EAPOL{Version: eapolVersion, Type: 0, Length: eap.Length},
108 eap,
109 )
110
111 bytes := buffer.Bytes()
112 return bytes
113}
114
115func extractEAP(pkt gopacket.Packet) (*layers.EAP, error) {
116 layerEAP := pkt.Layer(layers.LayerTypeEAP)
117 eap, _ := layerEAP.(*layers.EAP)
118 if eap == nil {
119 return nil, errors.New("Cannot extract EAP")
120 }
121 return eap, nil
122}
123
Matteo Scandolo075b1892019-10-07 12:11:07 -0700124func updateAuthFailed(onuId uint32, ponPortId uint32, serialNumber string, onuStateMachine *fsm.FSM) error {
125 if err := onuStateMachine.Event("auth_failed"); err != nil {
126 eapolLogger.WithFields(log.Fields{
127 "OnuId": onuId,
128 "IntfId": ponPortId,
129 "OnuSn": serialNumber,
130 }).Errorf("Error while transitioning ONU State %v", err)
131 return err
132 }
133 return nil
134}
135
Matteo Scandolo27428702019-10-11 16:21:16 -0700136func SendEapStart(onuId uint32, ponPortId uint32, serialNumber string, portNo uint32, macAddress net.HardwareAddr, onuStateMachine *fsm.FSM, stream bbsim.Stream) error {
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700137
138 // send the packet (hacked together)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700139 gemId, err := GetGemPortId(ponPortId, onuId)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700140 if err != nil {
Matteo Scandolo075b1892019-10-07 12:11:07 -0700141 eapolLogger.WithFields(log.Fields{
142 "OnuId": onuId,
143 "IntfId": ponPortId,
144 "OnuSn": serialNumber,
145 }).Errorf("Can't retrieve GemPortId: %s", err)
146
147 if err := updateAuthFailed(onuId, ponPortId, serialNumber, onuStateMachine); err != nil {
148 return err
149 }
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700150 return err
151 }
152
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700153 // TODO use createEAPOLPkt
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700154 buffer := gopacket.NewSerializeBuffer()
155 options := gopacket.SerializeOptions{}
156
157 ethernetLayer := &layers.Ethernet{
Matteo Scandolo86e8ce62019-10-11 12:03:10 -0700158 SrcMAC: macAddress,
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700159 DstMAC: net.HardwareAddr{0x01, 0x80, 0xC2, 0x00, 0x00, 0x03},
160 EthernetType: layers.EthernetTypeEAPOL,
161 }
162
163 gopacket.SerializeLayers(buffer, options,
164 ethernetLayer,
165 &layers.EAPOL{Version: eapolVersion, Type: 1, Length: 0},
166 )
167
168 msg := buffer.Bytes()
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700169 // TODO end createEAPOLPkt
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700170
171 data := &openolt.Indication_PktInd{
172 PktInd: &openolt.PacketIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700173 IntfType: "pon",
174 IntfId: ponPortId,
Matteo Scandolo075b1892019-10-07 12:11:07 -0700175 GemportId: uint32(gemId),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700176 Pkt: msg,
Matteo Scandolo27428702019-10-11 16:21:16 -0700177 PortNo: portNo,
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700178 },
179 }
Matteo Scandolo075b1892019-10-07 12:11:07 -0700180
181 err = stream.Send(&openolt.Indication{Data: data})
182 if err != nil {
183 eapolLogger.WithFields(log.Fields{
184 "OnuId": onuId,
185 "IntfId": ponPortId,
186 "OnuSn": serialNumber,
187 }).Errorf("Can't send EapStart Message: %s", err)
188
189 if err := updateAuthFailed(onuId, ponPortId, serialNumber, onuStateMachine); err != nil {
190 return err
191 }
192 return err
193 }
194
Matteo Scandolo27428702019-10-11 16:21:16 -0700195 eapolLogger.WithFields(log.Fields{
196 "OnuId": onuId,
197 "IntfId": ponPortId,
198 "OnuSn": serialNumber,
199 "PortNo": portNo,
200 }).Debugf("Sent EapStart packet")
Matteo Scandolo075b1892019-10-07 12:11:07 -0700201
202 if err := onuStateMachine.Event("eap_start_sent"); err != nil {
203 eapolLogger.WithFields(log.Fields{
204 "OnuId": onuId,
205 "IntfId": ponPortId,
206 "OnuSn": serialNumber,
207 }).Errorf("Error while transitioning ONU State %v", err)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700208 return err
209 }
210 return nil
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700211}
Matteo Scandolo075b1892019-10-07 12:11:07 -0700212
Matteo Scandolo27428702019-10-11 16:21:16 -0700213func HandleNextPacket(onuId uint32, ponPortId uint32, serialNumber string, portNo uint32, onuStateMachine *fsm.FSM, recvpkt gopacket.Packet, stream openolt.Openolt_EnableIndicationServer) {
214
Matteo Scandolo075b1892019-10-07 12:11:07 -0700215 eap, err := extractEAP(recvpkt)
216 if err != nil {
217 eapolLogger.Errorf("%s", err)
218 }
219
Matteo Scandolo27428702019-10-11 16:21:16 -0700220 log.WithFields(log.Fields{
221 "eap.Code": eap.Code,
222 "eap.Type": eap.Type,
223 "OnuId": onuId,
224 "IntfId": ponPortId,
225 "OnuSn": serialNumber,
226 }).Tracef("HandleNextPacket")
227
Matteo Scandolo075b1892019-10-07 12:11:07 -0700228 if eap.Code == layers.EAPCodeRequest && eap.Type == layers.EAPTypeIdentity {
229 reseap := createEAPIdentityResponse(eap.Id)
230 pkt := createEAPOLPkt(reseap, onuId, ponPortId)
231
232 msg := bbsim.ByteMsg{
233 IntfId: ponPortId,
234 OnuId: onuId,
235 Bytes: pkt,
236 }
237
Matteo Scandolo27428702019-10-11 16:21:16 -0700238 sendEapolPktIn(msg, portNo, stream)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700239 eapolLogger.WithFields(log.Fields{
240 "OnuId": onuId,
241 "IntfId": ponPortId,
242 "OnuSn": serialNumber,
Matteo Scandolo27428702019-10-11 16:21:16 -0700243 "PortNo": portNo,
244 }).Debugf("Sent EAPIdentityResponse packet")
Matteo Scandolo075b1892019-10-07 12:11:07 -0700245 if err := onuStateMachine.Event("eap_response_identity_sent"); err != nil {
246 eapolLogger.WithFields(log.Fields{
247 "OnuId": onuId,
248 "IntfId": ponPortId,
249 "OnuSn": serialNumber,
250 }).Errorf("Error while transitioning ONU State %v", err)
251 }
252
253 } else if eap.Code == layers.EAPCodeRequest && eap.Type == layers.EAPTypeOTP {
254 senddata := getMD5Data(eap)
255 senddata = append([]byte{0x10}, senddata...)
256 sendeap := createEAPChallengeResponse(eap.Id, senddata)
257 pkt := createEAPOLPkt(sendeap, onuId, ponPortId)
258
259 msg := bbsim.ByteMsg{
260 IntfId: ponPortId,
261 OnuId: onuId,
262 Bytes: pkt,
263 }
264
Matteo Scandolo27428702019-10-11 16:21:16 -0700265 sendEapolPktIn(msg, portNo, stream)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700266 eapolLogger.WithFields(log.Fields{
267 "OnuId": onuId,
268 "IntfId": ponPortId,
269 "OnuSn": serialNumber,
Matteo Scandolo27428702019-10-11 16:21:16 -0700270 "PortNo": portNo,
271 }).Debugf("Sent EAPChallengeResponse packet")
Matteo Scandolo075b1892019-10-07 12:11:07 -0700272 if err := onuStateMachine.Event("eap_response_challenge_sent"); err != nil {
273 eapolLogger.WithFields(log.Fields{
274 "OnuId": onuId,
275 "IntfId": ponPortId,
276 "OnuSn": serialNumber,
277 }).Errorf("Error while transitioning ONU State %v", err)
278 }
279 } else if eap.Code == layers.EAPCodeSuccess && eap.Type == layers.EAPTypeNone {
280 eapolLogger.WithFields(log.Fields{
281 "OnuId": onuId,
282 "IntfId": ponPortId,
283 "OnuSn": serialNumber,
Matteo Scandolo27428702019-10-11 16:21:16 -0700284 "PortNo": portNo,
285 }).Debugf("Received EAPSuccess packet")
Matteo Scandolo075b1892019-10-07 12:11:07 -0700286 if err := onuStateMachine.Event("eap_response_success_received"); err != nil {
287 eapolLogger.WithFields(log.Fields{
288 "OnuId": onuId,
289 "IntfId": ponPortId,
290 "OnuSn": serialNumber,
291 }).Errorf("Error while transitioning ONU State %v", err)
292 }
293 eapolLogger.WithFields(log.Fields{
294 "OnuId": onuId,
295 "IntfId": ponPortId,
296 "OnuSn": serialNumber,
297 }).Infof("EAPOL State machine completed")
298 return
299 }
300}