blob: 602a78e0f1c7bd304da457b284f8d7625f553852 [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
37
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070038func CreateWPASupplicant(onuId uint32, ponPortId uint32, serialNumber string, onuStateMachine *fsm.FSM, stream openolt.Openolt_EnableIndicationServer, pktOutCh chan *bbsim.ByteMsg) {
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070039 // NOTE pckOutCh is channel to listen on for packets received by VOLTHA
40 // the OLT device will publish messages on that channel
41
42 eapolLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070043 "OnuId": onuId,
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070044 "IntfId": ponPortId,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070045 "OnuSn": serialNumber,
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070046 }).Infof("EAPOL State Machine starting")
47
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070048 defer eapolLogger.WithFields(log.Fields{
49 "OnuId": onuId,
50 "IntfId": ponPortId,
51 "OnuSn": serialNumber,
52 }).Infof("EAPOL State machine completed")
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070053
54 if err := sendEapStart(onuId, ponPortId, serialNumber, stream); err != nil {
55 eapolLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070056 "OnuId": onuId,
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070057 "IntfId": ponPortId,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070058 "OnuSn": serialNumber,
59 }).Errorf("Can't send EapStart Message: %s", err)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070060 if err := onuStateMachine.Event("auth_failed"); err != nil {
61 eapolLogger.WithFields(log.Fields{
62 "OnuId": onuId,
63 "IntfId": ponPortId,
64 "OnuSn": serialNumber,
65 }).Errorf("Error while transitioning ONU State %v", err)
66 }
67 return
68 }
69 if err := onuStateMachine.Event("eap_start_sent"); err != nil {
70 eapolLogger.WithFields(log.Fields{
71 "OnuId": onuId,
72 "IntfId": ponPortId,
73 "OnuSn": serialNumber,
74 }).Errorf("Error while transitioning ONU State %v", err)
75 }
76
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070077 eapolLogger.WithFields(log.Fields{
78 "OnuId": onuId,
79 "IntfId": ponPortId,
80 "OnuSn": serialNumber,
81 }).Infof("Listening on eapolPktOutCh")
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070082
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070083 for msg := range pktOutCh {
84 recvpkt := gopacket.NewPacket(msg.Bytes, layers.LayerTypeEthernet, gopacket.Default)
85 eap, err := extractEAP(recvpkt)
86 if err != nil {
87 eapolLogger.Errorf("%s", err)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070088 }
89
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070090 if eap.Code == layers.EAPCodeRequest && eap.Type == layers.EAPTypeIdentity {
91 reseap := createEAPIdentityResponse(eap.Id)
92 pkt := createEAPOLPkt(reseap, onuId, ponPortId)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070093
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070094 msg := bbsim.ByteMsg{
95 IntfId: ponPortId,
96 OnuId: onuId,
97 Bytes: pkt,
98 }
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070099
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700100 sendEapolPktIn(msg, stream)
101 eapolLogger.WithFields(log.Fields{
102 "OnuId": onuId,
103 "IntfId": ponPortId,
104 "OnuSn": serialNumber,
105 }).Infof("Sent EAPIdentityResponse packet")
106 if err := onuStateMachine.Event("eap_response_identity_sent"); err != nil {
107 eapolLogger.WithFields(log.Fields{
108 "OnuId": onuId,
109 "IntfId": ponPortId,
110 "OnuSn": serialNumber,
111 }).Errorf("Error while transitioning ONU State %v", err)
112 }
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700113
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700114 } else if eap.Code == layers.EAPCodeRequest && eap.Type == layers.EAPTypeOTP {
115 senddata := getMD5Data(eap)
116 senddata = append([]byte{0x10}, senddata...)
117 sendeap := createEAPChallengeResponse(eap.Id, senddata)
118 pkt := createEAPOLPkt(sendeap, onuId, ponPortId)
119
120 msg := bbsim.ByteMsg{
121 IntfId: ponPortId,
122 OnuId: onuId,
123 Bytes: pkt,
124 }
125
126 sendEapolPktIn(msg, stream)
127 eapolLogger.WithFields(log.Fields{
128 "OnuId": onuId,
129 "IntfId": ponPortId,
130 "OnuSn": serialNumber,
131 }).Infof("Sent EAPChallengeResponse packet")
132 if err := onuStateMachine.Event("eap_response_challenge_sent"); err != nil {
133 eapolLogger.WithFields(log.Fields{
134 "OnuId": onuId,
135 "IntfId": ponPortId,
136 "OnuSn": serialNumber,
137 }).Errorf("Error while transitioning ONU State %v", err)
138 }
139 } else if eap.Code == layers.EAPCodeSuccess && eap.Type == layers.EAPTypeNone {
140 eapolLogger.WithFields(log.Fields{
141 "OnuId": onuId,
142 "IntfId": ponPortId,
143 "OnuSn": serialNumber,
144 }).Infof("Received EAPSuccess packet")
145 if err := onuStateMachine.Event("eap_response_success_received"); err != nil {
146 eapolLogger.WithFields(log.Fields{
147 "OnuId": onuId,
148 "IntfId": ponPortId,
149 "OnuSn": serialNumber,
150 }).Errorf("Error while transitioning ONU State %v", err)
151 }
152 return
153 }
154 }
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700155}
156
157func sendEapolPktIn(msg bbsim.ByteMsg, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700158 // FIXME unify sendDHCPPktIn and sendEapolPktIn methods
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700159 gemid, err := omci.GetGemPortId(msg.IntfId, msg.OnuId)
160 if err != nil {
161 eapolLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700162 "OnuId": msg.OnuId,
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700163 "IntfId": msg.IntfId,
164 }).Errorf("Can't retrieve GemPortId: %s", err)
165 return
166 }
167 data := &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{
168 IntfType: "pon", IntfId: msg.IntfId, GemportId: uint32(gemid), Pkt: msg.Bytes,
169 }}
170
171 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
172 eapolLogger.Errorf("Fail to send EAPOL PktInd indication. %v", err)
173 return
174 }
175}
176
177func getMD5Data(eap *layers.EAP) []byte {
178 i := byte(eap.Id)
179 C := []byte(eap.BaseLayer.Contents)[6:]
180 P := []byte{i, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64} // "password"
181 data := md5.Sum(append(P, C...))
182 ret := make([]byte, 16)
183 for j := 0; j < 16; j++ {
184 ret[j] = data[j]
185 }
186 return ret
187}
188
189func createEAPChallengeResponse(eapId uint8, payload []byte) *layers.EAP {
190 eap := layers.EAP{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700191 Code: layers.EAPCodeResponse,
192 Id: eapId,
193 Length: 22,
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700194 Type: layers.EAPTypeOTP,
195 TypeData: payload,
196 }
197 return &eap
198}
199
200func createEAPIdentityResponse(eapId uint8) *layers.EAP {
201 eap := layers.EAP{Code: layers.EAPCodeResponse,
202 Id: eapId,
203 Length: 9,
204 Type: layers.EAPTypeIdentity,
205 TypeData: []byte{0x75, 0x73, 0x65, 0x72}}
206 return &eap
207}
208
209func createEAPOLPkt(eap *layers.EAP, onuId uint32, intfId uint32) []byte {
210 buffer := gopacket.NewSerializeBuffer()
211 options := gopacket.SerializeOptions{}
212
213 ethernetLayer := &layers.Ethernet{
214 SrcMAC: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(intfId), byte(onuId)},
215 DstMAC: net.HardwareAddr{0x01, 0x80, 0xC2, 0x00, 0x00, 0x03},
216 EthernetType: layers.EthernetTypeEAPOL,
217 }
218
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700219 gopacket.SerializeLayers(buffer, options,
220 ethernetLayer,
221 &layers.EAPOL{Version: eapolVersion, Type: 0, Length: eap.Length},
222 eap,
223 )
224
225 bytes := buffer.Bytes()
226 return bytes
227}
228
229func extractEAP(pkt gopacket.Packet) (*layers.EAP, error) {
230 layerEAP := pkt.Layer(layers.LayerTypeEAP)
231 eap, _ := layerEAP.(*layers.EAP)
232 if eap == nil {
233 return nil, errors.New("Cannot extract EAP")
234 }
235 return eap, nil
236}
237
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700238var sendEapStart = func(onuId uint32, ponPortId uint32, serialNumber string, stream openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700239
240 // send the packet (hacked together)
241 gemid, err := omci.GetGemPortId(ponPortId, onuId)
242 if err != nil {
243 return err
244 }
245
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700246 // TODO use createEAPOLPkt
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700247 buffer := gopacket.NewSerializeBuffer()
248 options := gopacket.SerializeOptions{}
249
250 ethernetLayer := &layers.Ethernet{
251 SrcMAC: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(ponPortId), byte(onuId)}, // TODO move the SrcMAC in the ONU Device
252 DstMAC: net.HardwareAddr{0x01, 0x80, 0xC2, 0x00, 0x00, 0x03},
253 EthernetType: layers.EthernetTypeEAPOL,
254 }
255
256 gopacket.SerializeLayers(buffer, options,
257 ethernetLayer,
258 &layers.EAPOL{Version: eapolVersion, Type: 1, Length: 0},
259 )
260
261 msg := buffer.Bytes()
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700262 // TODO end createEAPOLPkt
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700263
264 data := &openolt.Indication_PktInd{
265 PktInd: &openolt.PacketIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700266 IntfType: "pon",
267 IntfId: ponPortId,
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700268 GemportId: uint32(gemid),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700269 Pkt: msg,
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700270 },
271 }
272 // end of hacked (move in an EAPOL state machine)
273 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
274 eapolLogger.Errorf("Fail to send EAPOL PktInd indication. %v", err)
275 return err
276 }
277 return nil
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700278}