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 |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 37 | var GetGemPortId = omci.GetGemPortId |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 38 | |
| 39 | func sendEapolPktIn(msg bbsim.ByteMsg, stream openolt.Openolt_EnableIndicationServer) { |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 40 | // FIXME unify sendDHCPPktIn and sendEapolPktIn methods |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 41 | gemid, err := omci.GetGemPortId(msg.IntfId, msg.OnuId) |
| 42 | if err != nil { |
| 43 | eapolLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 44 | "OnuId": msg.OnuId, |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 45 | "IntfId": msg.IntfId, |
| 46 | }).Errorf("Can't retrieve GemPortId: %s", err) |
| 47 | return |
| 48 | } |
| 49 | data := &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{ |
| 50 | IntfType: "pon", IntfId: msg.IntfId, GemportId: uint32(gemid), Pkt: msg.Bytes, |
| 51 | }} |
| 52 | |
| 53 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
| 54 | eapolLogger.Errorf("Fail to send EAPOL PktInd indication. %v", err) |
| 55 | return |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func getMD5Data(eap *layers.EAP) []byte { |
| 60 | i := byte(eap.Id) |
| 61 | C := []byte(eap.BaseLayer.Contents)[6:] |
| 62 | P := []byte{i, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64} // "password" |
| 63 | data := md5.Sum(append(P, C...)) |
| 64 | ret := make([]byte, 16) |
| 65 | for j := 0; j < 16; j++ { |
| 66 | ret[j] = data[j] |
| 67 | } |
| 68 | return ret |
| 69 | } |
| 70 | |
| 71 | func createEAPChallengeResponse(eapId uint8, payload []byte) *layers.EAP { |
| 72 | eap := layers.EAP{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 73 | Code: layers.EAPCodeResponse, |
| 74 | Id: eapId, |
| 75 | Length: 22, |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 76 | Type: layers.EAPTypeOTP, |
| 77 | TypeData: payload, |
| 78 | } |
| 79 | return &eap |
| 80 | } |
| 81 | |
| 82 | func createEAPIdentityResponse(eapId uint8) *layers.EAP { |
| 83 | eap := layers.EAP{Code: layers.EAPCodeResponse, |
| 84 | Id: eapId, |
| 85 | Length: 9, |
| 86 | Type: layers.EAPTypeIdentity, |
| 87 | TypeData: []byte{0x75, 0x73, 0x65, 0x72}} |
| 88 | return &eap |
| 89 | } |
| 90 | |
| 91 | func createEAPOLPkt(eap *layers.EAP, onuId uint32, intfId uint32) []byte { |
| 92 | buffer := gopacket.NewSerializeBuffer() |
| 93 | options := gopacket.SerializeOptions{} |
| 94 | |
| 95 | ethernetLayer := &layers.Ethernet{ |
| 96 | SrcMAC: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(intfId), byte(onuId)}, |
| 97 | DstMAC: net.HardwareAddr{0x01, 0x80, 0xC2, 0x00, 0x00, 0x03}, |
| 98 | EthernetType: layers.EthernetTypeEAPOL, |
| 99 | } |
| 100 | |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 101 | gopacket.SerializeLayers(buffer, options, |
| 102 | ethernetLayer, |
| 103 | &layers.EAPOL{Version: eapolVersion, Type: 0, Length: eap.Length}, |
| 104 | eap, |
| 105 | ) |
| 106 | |
| 107 | bytes := buffer.Bytes() |
| 108 | return bytes |
| 109 | } |
| 110 | |
| 111 | func extractEAP(pkt gopacket.Packet) (*layers.EAP, error) { |
| 112 | layerEAP := pkt.Layer(layers.LayerTypeEAP) |
| 113 | eap, _ := layerEAP.(*layers.EAP) |
| 114 | if eap == nil { |
| 115 | return nil, errors.New("Cannot extract EAP") |
| 116 | } |
| 117 | return eap, nil |
| 118 | } |
| 119 | |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 120 | func updateAuthFailed(onuId uint32, ponPortId uint32, serialNumber string, onuStateMachine *fsm.FSM) error { |
| 121 | if err := onuStateMachine.Event("auth_failed"); err != nil { |
| 122 | eapolLogger.WithFields(log.Fields{ |
| 123 | "OnuId": onuId, |
| 124 | "IntfId": ponPortId, |
| 125 | "OnuSn": serialNumber, |
| 126 | }).Errorf("Error while transitioning ONU State %v", err) |
| 127 | return err |
| 128 | } |
| 129 | return nil |
| 130 | } |
| 131 | |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame^] | 132 | func SendEapStart(onuId uint32, ponPortId uint32, serialNumber string, macAddress net.HardwareAddr, onuStateMachine *fsm.FSM, stream openolt.Openolt_EnableIndicationServer) error { |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 133 | |
| 134 | // send the packet (hacked together) |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 135 | gemId, err := GetGemPortId(ponPortId, onuId) |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 136 | if err != nil { |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 137 | eapolLogger.WithFields(log.Fields{ |
| 138 | "OnuId": onuId, |
| 139 | "IntfId": ponPortId, |
| 140 | "OnuSn": serialNumber, |
| 141 | }).Errorf("Can't retrieve GemPortId: %s", err) |
| 142 | |
| 143 | if err := updateAuthFailed(onuId, ponPortId, serialNumber, onuStateMachine); err != nil { |
| 144 | return err |
| 145 | } |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 146 | return err |
| 147 | } |
| 148 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 149 | // TODO use createEAPOLPkt |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 150 | buffer := gopacket.NewSerializeBuffer() |
| 151 | options := gopacket.SerializeOptions{} |
| 152 | |
| 153 | ethernetLayer := &layers.Ethernet{ |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame^] | 154 | SrcMAC: macAddress, |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 155 | DstMAC: net.HardwareAddr{0x01, 0x80, 0xC2, 0x00, 0x00, 0x03}, |
| 156 | EthernetType: layers.EthernetTypeEAPOL, |
| 157 | } |
| 158 | |
| 159 | gopacket.SerializeLayers(buffer, options, |
| 160 | ethernetLayer, |
| 161 | &layers.EAPOL{Version: eapolVersion, Type: 1, Length: 0}, |
| 162 | ) |
| 163 | |
| 164 | msg := buffer.Bytes() |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 165 | // TODO end createEAPOLPkt |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 166 | |
| 167 | data := &openolt.Indication_PktInd{ |
| 168 | PktInd: &openolt.PacketIndication{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 169 | IntfType: "pon", |
| 170 | IntfId: ponPortId, |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 171 | GemportId: uint32(gemId), |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 172 | Pkt: msg, |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 173 | }, |
| 174 | } |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 175 | |
| 176 | err = stream.Send(&openolt.Indication{Data: data}) |
| 177 | if err != nil { |
| 178 | eapolLogger.WithFields(log.Fields{ |
| 179 | "OnuId": onuId, |
| 180 | "IntfId": ponPortId, |
| 181 | "OnuSn": serialNumber, |
| 182 | }).Errorf("Can't send EapStart Message: %s", err) |
| 183 | |
| 184 | if err := updateAuthFailed(onuId, ponPortId, serialNumber, onuStateMachine); err != nil { |
| 185 | return err |
| 186 | } |
| 187 | return err |
| 188 | } |
| 189 | |
| 190 | if err != nil { |
| 191 | |
| 192 | } |
| 193 | |
| 194 | if err := onuStateMachine.Event("eap_start_sent"); err != nil { |
| 195 | eapolLogger.WithFields(log.Fields{ |
| 196 | "OnuId": onuId, |
| 197 | "IntfId": ponPortId, |
| 198 | "OnuSn": serialNumber, |
| 199 | }).Errorf("Error while transitioning ONU State %v", err) |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 200 | return err |
| 201 | } |
| 202 | return nil |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 203 | } |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 204 | |
| 205 | func HandleNextPacket(onuId uint32, ponPortId uint32, serialNumber string, onuStateMachine *fsm.FSM, recvpkt gopacket.Packet, stream openolt.Openolt_EnableIndicationServer) { |
| 206 | eap, err := extractEAP(recvpkt) |
| 207 | if err != nil { |
| 208 | eapolLogger.Errorf("%s", err) |
| 209 | } |
| 210 | |
| 211 | if eap.Code == layers.EAPCodeRequest && eap.Type == layers.EAPTypeIdentity { |
| 212 | reseap := createEAPIdentityResponse(eap.Id) |
| 213 | pkt := createEAPOLPkt(reseap, onuId, ponPortId) |
| 214 | |
| 215 | msg := bbsim.ByteMsg{ |
| 216 | IntfId: ponPortId, |
| 217 | OnuId: onuId, |
| 218 | Bytes: pkt, |
| 219 | } |
| 220 | |
| 221 | sendEapolPktIn(msg, stream) |
| 222 | eapolLogger.WithFields(log.Fields{ |
| 223 | "OnuId": onuId, |
| 224 | "IntfId": ponPortId, |
| 225 | "OnuSn": serialNumber, |
| 226 | }).Infof("Sent EAPIdentityResponse packet") |
| 227 | if err := onuStateMachine.Event("eap_response_identity_sent"); err != nil { |
| 228 | eapolLogger.WithFields(log.Fields{ |
| 229 | "OnuId": onuId, |
| 230 | "IntfId": ponPortId, |
| 231 | "OnuSn": serialNumber, |
| 232 | }).Errorf("Error while transitioning ONU State %v", err) |
| 233 | } |
| 234 | |
| 235 | } else if eap.Code == layers.EAPCodeRequest && eap.Type == layers.EAPTypeOTP { |
| 236 | senddata := getMD5Data(eap) |
| 237 | senddata = append([]byte{0x10}, senddata...) |
| 238 | sendeap := createEAPChallengeResponse(eap.Id, senddata) |
| 239 | pkt := createEAPOLPkt(sendeap, onuId, ponPortId) |
| 240 | |
| 241 | msg := bbsim.ByteMsg{ |
| 242 | IntfId: ponPortId, |
| 243 | OnuId: onuId, |
| 244 | Bytes: pkt, |
| 245 | } |
| 246 | |
| 247 | sendEapolPktIn(msg, stream) |
| 248 | eapolLogger.WithFields(log.Fields{ |
| 249 | "OnuId": onuId, |
| 250 | "IntfId": ponPortId, |
| 251 | "OnuSn": serialNumber, |
| 252 | }).Infof("Sent EAPChallengeResponse packet") |
| 253 | if err := onuStateMachine.Event("eap_response_challenge_sent"); err != nil { |
| 254 | eapolLogger.WithFields(log.Fields{ |
| 255 | "OnuId": onuId, |
| 256 | "IntfId": ponPortId, |
| 257 | "OnuSn": serialNumber, |
| 258 | }).Errorf("Error while transitioning ONU State %v", err) |
| 259 | } |
| 260 | } else if eap.Code == layers.EAPCodeSuccess && eap.Type == layers.EAPTypeNone { |
| 261 | eapolLogger.WithFields(log.Fields{ |
| 262 | "OnuId": onuId, |
| 263 | "IntfId": ponPortId, |
| 264 | "OnuSn": serialNumber, |
| 265 | }).Infof("Received EAPSuccess packet") |
| 266 | if err := onuStateMachine.Event("eap_response_success_received"); err != nil { |
| 267 | eapolLogger.WithFields(log.Fields{ |
| 268 | "OnuId": onuId, |
| 269 | "IntfId": ponPortId, |
| 270 | "OnuSn": serialNumber, |
| 271 | }).Errorf("Error while transitioning ONU State %v", err) |
| 272 | } |
| 273 | eapolLogger.WithFields(log.Fields{ |
| 274 | "OnuId": onuId, |
| 275 | "IntfId": ponPortId, |
| 276 | "OnuSn": serialNumber, |
| 277 | }).Infof("EAPOL State machine completed") |
| 278 | return |
| 279 | } |
| 280 | } |