blob: d32999ab78efd06e4bf298062dcaa0ea5ec83996 [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"
22 bbsim "github.com/opencord/bbsim/internal/bbsim/types"
23 "github.com/google/gopacket"
24 "github.com/google/gopacket/layers"
25 "github.com/looplab/fsm"
26 omci "github.com/opencord/omci-sim"
27 "github.com/opencord/voltha-protos/go/openolt"
28 log "github.com/sirupsen/logrus"
29 "net"
30 "sync"
31)
32
33var eapolLogger = log.WithFields(log.Fields{
34 "module": "EAPOL",
35})
36
37var eapolVersion uint8 = 1
38
39func CreateWPASupplicant(onuId uint32, ponPortId uint32, serialNumber *openolt.SerialNumber, onuStateMachine *fsm.FSM, stream openolt.Openolt_EnableIndicationServer, pktOutCh chan *bbsim.ByteMsg) {
40 // NOTE pckOutCh is channel to listen on for packets received by VOLTHA
41 // the OLT device will publish messages on that channel
42
43 eapolLogger.WithFields(log.Fields{
44 "OnuId": onuId,
45 "IntfId": ponPortId,
46 "OnuSn": serialNumber,
47 }).Infof("EAPOL State Machine starting")
48
49 var wg sync.WaitGroup
50 wg.Add(1)
51
52 if err := sendEapStart(onuId, ponPortId, serialNumber, stream); err != nil {
53 eapolLogger.WithFields(log.Fields{
54 "OnuId": onuId,
55 "IntfId": ponPortId,
56 "OnuSn": serialNumber,
57 }).Errorf("Can't retrieve GemPortId: %s", err)
58 if err := onuStateMachine.Event("auth_failed"); err != nil {
59 eapolLogger.WithFields(log.Fields{
60 "OnuId": onuId,
61 "IntfId": ponPortId,
62 "OnuSn": serialNumber,
63 }).Errorf("Error while transitioning ONU State %v", err)
64 }
65 return
66 }
67 if err := onuStateMachine.Event("eap_start_sent"); err != nil {
68 eapolLogger.WithFields(log.Fields{
69 "OnuId": onuId,
70 "IntfId": ponPortId,
71 "OnuSn": serialNumber,
72 }).Errorf("Error while transitioning ONU State %v", err)
73 }
74
75 // NOTE do we really need a thread here?
76 go func() {
77 eapolLogger.WithFields(log.Fields{
78 "OnuId": onuId,
79 "IntfId": ponPortId,
80 "OnuSn": serialNumber,
81 }).Infof("Listening on eapolPktOutCh")
82 for msg := range pktOutCh {
83 recvpkt := gopacket.NewPacket(msg.Bytes, layers.LayerTypeEthernet, gopacket.Default)
84 eap, err := extractEAP(recvpkt)
85 if err != nil {
86 eapolLogger.Errorf("%s", err)
87 }
88
89 if eap.Code == layers.EAPCodeRequest && eap.Type == layers.EAPTypeIdentity {
90 reseap := createEAPIdentityResponse(eap.Id)
91 pkt := createEAPOLPkt(reseap, onuId, ponPortId)
92
93 msg := bbsim.ByteMsg{
94 IntfId: ponPortId,
95 OnuId: onuId,
96 Bytes: pkt,
97 }
98
99 sendEapolPktIn(msg, stream)
100 eapolLogger.WithFields(log.Fields{
101 "OnuId": onuId,
102 "IntfId": ponPortId,
103 "OnuSn": serialNumber,
104 }).Infof("Sent EAPIdentityResponse packet")
105 if err := onuStateMachine.Event("eap_resonse_identity_sent"); err != nil {
106 eapolLogger.WithFields(log.Fields{
107 "OnuId": onuId,
108 "IntfId": ponPortId,
109 "OnuSn": serialNumber,
110 }).Errorf("Error while transitioning ONU State %v", err)
111 }
112
113 } else if eap.Code == layers.EAPCodeRequest && eap.Type == layers.EAPTypeOTP {
114 senddata := getMD5Data(eap)
115 senddata = append([]byte{0x10}, senddata...)
116 sendeap := createEAPChallengeResponse(eap.Id, senddata)
117 pkt := createEAPOLPkt(sendeap, onuId, ponPortId)
118
119 msg := bbsim.ByteMsg{
120 IntfId: ponPortId,
121 OnuId: onuId,
122 Bytes: pkt,
123 }
124
125 sendEapolPktIn(msg, stream)
126 eapolLogger.WithFields(log.Fields{
127 "OnuId": onuId,
128 "IntfId": ponPortId,
129 "OnuSn": serialNumber,
130 }).Infof("Sent EAPChallengeResponse packet")
131 if err := onuStateMachine.Event("eap_resonse_challenge_sent"); err != nil {
132 eapolLogger.WithFields(log.Fields{
133 "OnuId": onuId,
134 "IntfId": ponPortId,
135 "OnuSn": serialNumber,
136 }).Errorf("Error while transitioning ONU State %v", err)
137 }
138 } else if eap.Code == layers.EAPCodeSuccess && eap.Type == layers.EAPTypeNone {
139 eapolLogger.WithFields(log.Fields{
140 "OnuId": onuId,
141 "IntfId": ponPortId,
142 "OnuSn": serialNumber,
143 }).Infof("Received EAPSuccess packet")
144 if err := onuStateMachine.Event("eap_resonse_success_received"); err != nil {
145 eapolLogger.WithFields(log.Fields{
146 "OnuId": onuId,
147 "IntfId": ponPortId,
148 "OnuSn": serialNumber,
149 }).Errorf("Error while transitioning ONU State %v", err)
150 }
151 wg.Done()
152 }
153
154 }
155
156
157 }()
158
159 defer eapolLogger.WithFields(log.Fields{
160 "OnuId": onuId,
161 "IntfId": ponPortId,
162 "OnuSn": serialNumber,
163 }).Infof("EAPOL State machine completed")
164
165 wg.Wait()
166}
167
168func sendEapolPktIn(msg bbsim.ByteMsg, stream openolt.Openolt_EnableIndicationServer) {
169 gemid, err := omci.GetGemPortId(msg.IntfId, msg.OnuId)
170 if err != nil {
171 eapolLogger.WithFields(log.Fields{
172 "OnuId": msg.OnuId,
173 "IntfId": msg.IntfId,
174 }).Errorf("Can't retrieve GemPortId: %s", err)
175 return
176 }
177 data := &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{
178 IntfType: "pon", IntfId: msg.IntfId, GemportId: uint32(gemid), Pkt: msg.Bytes,
179 }}
180
181 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
182 eapolLogger.Errorf("Fail to send EAPOL PktInd indication. %v", err)
183 return
184 }
185}
186
187func getMD5Data(eap *layers.EAP) []byte {
188 i := byte(eap.Id)
189 C := []byte(eap.BaseLayer.Contents)[6:]
190 P := []byte{i, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64} // "password"
191 data := md5.Sum(append(P, C...))
192 ret := make([]byte, 16)
193 for j := 0; j < 16; j++ {
194 ret[j] = data[j]
195 }
196 return ret
197}
198
199func createEAPChallengeResponse(eapId uint8, payload []byte) *layers.EAP {
200 eap := layers.EAP{
201 Code: layers.EAPCodeResponse,
202 Id: eapId,
203 Length: 22,
204 Type: layers.EAPTypeOTP,
205 TypeData: payload,
206 }
207 return &eap
208}
209
210func createEAPIdentityResponse(eapId uint8) *layers.EAP {
211 eap := layers.EAP{Code: layers.EAPCodeResponse,
212 Id: eapId,
213 Length: 9,
214 Type: layers.EAPTypeIdentity,
215 TypeData: []byte{0x75, 0x73, 0x65, 0x72}}
216 return &eap
217}
218
219func createEAPOLPkt(eap *layers.EAP, onuId uint32, intfId uint32) []byte {
220 buffer := gopacket.NewSerializeBuffer()
221 options := gopacket.SerializeOptions{}
222
223 ethernetLayer := &layers.Ethernet{
224 SrcMAC: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(intfId), byte(onuId)},
225 DstMAC: net.HardwareAddr{0x01, 0x80, 0xC2, 0x00, 0x00, 0x03},
226 EthernetType: layers.EthernetTypeEAPOL,
227 }
228
229
230 gopacket.SerializeLayers(buffer, options,
231 ethernetLayer,
232 &layers.EAPOL{Version: eapolVersion, Type: 0, Length: eap.Length},
233 eap,
234 )
235
236 bytes := buffer.Bytes()
237 return bytes
238}
239
240func extractEAP(pkt gopacket.Packet) (*layers.EAP, error) {
241 layerEAP := pkt.Layer(layers.LayerTypeEAP)
242 eap, _ := layerEAP.(*layers.EAP)
243 if eap == nil {
244 return nil, errors.New("Cannot extract EAP")
245 }
246 return eap, nil
247}
248
249var sendEapStart = func(onuId uint32, ponPortId uint32, serialNumber *openolt.SerialNumber, stream openolt.Openolt_EnableIndicationServer) error {
250
251 // send the packet (hacked together)
252 gemid, err := omci.GetGemPortId(ponPortId, onuId)
253 if err != nil {
254 return err
255 }
256
257 buffer := gopacket.NewSerializeBuffer()
258 options := gopacket.SerializeOptions{}
259
260 ethernetLayer := &layers.Ethernet{
261 SrcMAC: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(ponPortId), byte(onuId)}, // TODO move the SrcMAC in the ONU Device
262 DstMAC: net.HardwareAddr{0x01, 0x80, 0xC2, 0x00, 0x00, 0x03},
263 EthernetType: layers.EthernetTypeEAPOL,
264 }
265
266 gopacket.SerializeLayers(buffer, options,
267 ethernetLayer,
268 &layers.EAPOL{Version: eapolVersion, Type: 1, Length: 0},
269 )
270
271 msg := buffer.Bytes()
272
273 data := &openolt.Indication_PktInd{
274 PktInd: &openolt.PacketIndication{
275 IntfType: "pon",
276 IntfId: ponPortId,
277 GemportId: uint32(gemid),
278 Pkt: msg,
279 },
280 }
281 // end of hacked (move in an EAPOL state machine)
282 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
283 eapolLogger.Errorf("Fail to send EAPOL PktInd indication. %v", err)
284 return err
285 }
286 return nil
287}