Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package eapol |
| 18 | |
| 19 | import ( |
| 20 | "crypto/md5" |
| 21 | "errors" |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 22 | "github.com/google/gopacket" |
| 23 | "github.com/google/gopacket/layers" |
| 24 | "github.com/looplab/fsm" |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 25 | bbsim "github.com/opencord/bbsim/internal/bbsim/types" |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 26 | omci "github.com/opencord/omci-sim" |
| 27 | "github.com/opencord/voltha-protos/go/openolt" |
| 28 | log "github.com/sirupsen/logrus" |
| 29 | "net" |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | var eapolLogger = log.WithFields(log.Fields{ |
| 33 | "module": "EAPOL", |
| 34 | }) |
| 35 | |
| 36 | var eapolVersion uint8 = 1 |
| 37 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 38 | func CreateWPASupplicant(onuId uint32, ponPortId uint32, serialNumber string, onuStateMachine *fsm.FSM, stream openolt.Openolt_EnableIndicationServer, pktOutCh chan *bbsim.ByteMsg) { |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 39 | // 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 Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 43 | "OnuId": onuId, |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 44 | "IntfId": ponPortId, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 45 | "OnuSn": serialNumber, |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 46 | }).Infof("EAPOL State Machine starting") |
| 47 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 48 | defer eapolLogger.WithFields(log.Fields{ |
| 49 | "OnuId": onuId, |
| 50 | "IntfId": ponPortId, |
| 51 | "OnuSn": serialNumber, |
| 52 | }).Infof("EAPOL State machine completed") |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 53 | |
| 54 | if err := sendEapStart(onuId, ponPortId, serialNumber, stream); err != nil { |
| 55 | eapolLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 56 | "OnuId": onuId, |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 57 | "IntfId": ponPortId, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 58 | "OnuSn": serialNumber, |
| 59 | }).Errorf("Can't send EapStart Message: %s", err) |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 60 | 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 Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 77 | eapolLogger.WithFields(log.Fields{ |
| 78 | "OnuId": onuId, |
| 79 | "IntfId": ponPortId, |
| 80 | "OnuSn": serialNumber, |
| 81 | }).Infof("Listening on eapolPktOutCh") |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 82 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 83 | 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 Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 90 | if eap.Code == layers.EAPCodeRequest && eap.Type == layers.EAPTypeIdentity { |
| 91 | reseap := createEAPIdentityResponse(eap.Id) |
| 92 | pkt := createEAPOLPkt(reseap, onuId, ponPortId) |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 93 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 94 | msg := bbsim.ByteMsg{ |
| 95 | IntfId: ponPortId, |
| 96 | OnuId: onuId, |
| 97 | Bytes: pkt, |
| 98 | } |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 99 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 100 | 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 Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 113 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 114 | } 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 Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | func sendEapolPktIn(msg bbsim.ByteMsg, stream openolt.Openolt_EnableIndicationServer) { |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 158 | // FIXME unify sendDHCPPktIn and sendEapolPktIn methods |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 159 | gemid, err := omci.GetGemPortId(msg.IntfId, msg.OnuId) |
| 160 | if err != nil { |
| 161 | eapolLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 162 | "OnuId": msg.OnuId, |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 163 | "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 | |
| 177 | func 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 | |
| 189 | func createEAPChallengeResponse(eapId uint8, payload []byte) *layers.EAP { |
| 190 | eap := layers.EAP{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 191 | Code: layers.EAPCodeResponse, |
| 192 | Id: eapId, |
| 193 | Length: 22, |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 194 | Type: layers.EAPTypeOTP, |
| 195 | TypeData: payload, |
| 196 | } |
| 197 | return &eap |
| 198 | } |
| 199 | |
| 200 | func 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 | |
| 209 | func 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 Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 219 | 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 | |
| 229 | func 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 Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 238 | var sendEapStart = func(onuId uint32, ponPortId uint32, serialNumber string, stream openolt.Openolt_EnableIndicationServer) error { |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 239 | |
| 240 | // send the packet (hacked together) |
| 241 | gemid, err := omci.GetGemPortId(ponPortId, onuId) |
| 242 | if err != nil { |
| 243 | return err |
| 244 | } |
| 245 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 246 | // TODO use createEAPOLPkt |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 247 | 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 Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 262 | // TODO end createEAPOLPkt |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 263 | |
| 264 | data := &openolt.Indication_PktInd{ |
| 265 | PktInd: &openolt.PacketIndication{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 266 | IntfType: "pon", |
| 267 | IntfId: ponPortId, |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 268 | GemportId: uint32(gemid), |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 269 | Pkt: msg, |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 270 | }, |
| 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 Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 278 | } |