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 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame^] | 39 | func sendEapolPktIn(msg bbsim.ByteMsg, portNo uint32, 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{ |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame^] | 50 | IntfType: "pon", |
| 51 | IntfId: msg.IntfId, |
| 52 | GemportId: uint32(gemid), |
| 53 | Pkt: msg.Bytes, |
| 54 | PortNo: portNo, |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 55 | }} |
| 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 | |
| 63 | func 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 | |
| 75 | func createEAPChallengeResponse(eapId uint8, payload []byte) *layers.EAP { |
| 76 | eap := layers.EAP{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 77 | Code: layers.EAPCodeResponse, |
| 78 | Id: eapId, |
| 79 | Length: 22, |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 80 | Type: layers.EAPTypeOTP, |
| 81 | TypeData: payload, |
| 82 | } |
| 83 | return &eap |
| 84 | } |
| 85 | |
| 86 | func 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 | |
| 95 | func 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 Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 105 | 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 | |
| 115 | func 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 Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 124 | func 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 Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame^] | 136 | func SendEapStart(onuId uint32, ponPortId uint32, serialNumber string, portNo uint32, macAddress net.HardwareAddr, onuStateMachine *fsm.FSM, stream bbsim.Stream) error { |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 137 | |
| 138 | // send the packet (hacked together) |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 139 | gemId, err := GetGemPortId(ponPortId, onuId) |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 140 | if err != nil { |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 141 | 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 Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 150 | return err |
| 151 | } |
| 152 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 153 | // TODO use createEAPOLPkt |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 154 | buffer := gopacket.NewSerializeBuffer() |
| 155 | options := gopacket.SerializeOptions{} |
| 156 | |
| 157 | ethernetLayer := &layers.Ethernet{ |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 158 | SrcMAC: macAddress, |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 159 | 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 Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 169 | // TODO end createEAPOLPkt |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 170 | |
| 171 | data := &openolt.Indication_PktInd{ |
| 172 | PktInd: &openolt.PacketIndication{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 173 | IntfType: "pon", |
| 174 | IntfId: ponPortId, |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 175 | GemportId: uint32(gemId), |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 176 | Pkt: msg, |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame^] | 177 | PortNo: portNo, |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 178 | }, |
| 179 | } |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 180 | |
| 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 Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame^] | 195 | eapolLogger.WithFields(log.Fields{ |
| 196 | "OnuId": onuId, |
| 197 | "IntfId": ponPortId, |
| 198 | "OnuSn": serialNumber, |
| 199 | "PortNo": portNo, |
| 200 | }).Debugf("Sent EapStart packet") |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 201 | |
| 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 Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 208 | return err |
| 209 | } |
| 210 | return nil |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 211 | } |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 212 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame^] | 213 | func HandleNextPacket(onuId uint32, ponPortId uint32, serialNumber string, portNo uint32, onuStateMachine *fsm.FSM, recvpkt gopacket.Packet, stream openolt.Openolt_EnableIndicationServer) { |
| 214 | |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 215 | eap, err := extractEAP(recvpkt) |
| 216 | if err != nil { |
| 217 | eapolLogger.Errorf("%s", err) |
| 218 | } |
| 219 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame^] | 220 | 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 Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 228 | 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 Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame^] | 238 | sendEapolPktIn(msg, portNo, stream) |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 239 | eapolLogger.WithFields(log.Fields{ |
| 240 | "OnuId": onuId, |
| 241 | "IntfId": ponPortId, |
| 242 | "OnuSn": serialNumber, |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame^] | 243 | "PortNo": portNo, |
| 244 | }).Debugf("Sent EAPIdentityResponse packet") |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 245 | 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 Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame^] | 265 | sendEapolPktIn(msg, portNo, stream) |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 266 | eapolLogger.WithFields(log.Fields{ |
| 267 | "OnuId": onuId, |
| 268 | "IntfId": ponPortId, |
| 269 | "OnuSn": serialNumber, |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame^] | 270 | "PortNo": portNo, |
| 271 | }).Debugf("Sent EAPChallengeResponse packet") |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 272 | 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 Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame^] | 284 | "PortNo": portNo, |
| 285 | }).Debugf("Received EAPSuccess packet") |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 286 | 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 | } |