Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [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 core |
| 18 | |
| 19 | import ( |
| 20 | "context" |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 21 | "crypto/md5" |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 22 | "encoding/hex" |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 23 | "errors" |
| 24 | "fmt" |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 25 | "net" |
| 26 | "sync" |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 27 | "time" |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 28 | |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 29 | "github.com/google/gopacket" |
| 30 | "github.com/google/gopacket/layers" |
Zack Williams | 2abf393 | 2019-08-05 14:07:05 -0700 | [diff] [blame] | 31 | "github.com/opencord/voltha-bbsim/common/logger" |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 32 | log "github.com/sirupsen/logrus" |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 33 | ) |
| 34 | |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 35 | type clientState int |
| 36 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 37 | // Constants for eapol states |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 38 | const ( |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 39 | EapStart clientState = iota + 1 // TODO: This state definition should support 802.1X |
| 40 | EapRespid |
| 41 | EapRespcha |
| 42 | EapSuccess |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 43 | ) |
| 44 | |
Matt Jeanneret | 7c9c5f2 | 2019-08-09 14:40:12 -0400 | [diff] [blame] | 45 | func (eap clientState) String() string { |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 46 | return [...]string{"EAP_START", "EAP_RESPID", "EAP_RESPCHA", "EAP_SUCCESS"}[eap] |
| 47 | } |
| 48 | |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 49 | type eapResponder struct { |
| 50 | clients map[clientKey]*eapClientInstance |
| 51 | eapolIn chan *byteMsg |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 52 | } |
| 53 | |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 54 | type clientInstance interface { |
| 55 | transitState(cur clientState, recvbytes []byte) (next clientState, sendbytes []byte, err error) |
| 56 | getState() clientState |
| 57 | getKey() clientKey |
| 58 | } |
| 59 | |
| 60 | type eapClientInstance struct { |
| 61 | key clientKey |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 62 | srcaddr *net.HardwareAddr |
| 63 | version uint8 |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 64 | curID uint8 |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 65 | curState clientState |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 66 | } |
| 67 | |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 68 | type clientKey struct { |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 69 | intfid uint32 |
| 70 | onuid uint32 |
| 71 | } |
| 72 | |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 73 | var resp *eapResponder |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 74 | var once sync.Once |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 75 | |
| 76 | func getEAPResponder() *eapResponder { |
| 77 | once.Do(func() { |
| 78 | resp = &eapResponder{clients: make(map[clientKey]*eapClientInstance), eapolIn: nil} |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 79 | }) |
| 80 | return resp |
| 81 | } |
| 82 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 83 | // RunEapolResponder starts go routine which processes and responds for received eapol messages |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 84 | func RunEapolResponder(ctx context.Context, eapolOut chan *byteMsg, eapolIn chan *byteMsg, errch chan error) { |
| 85 | responder := getEAPResponder() |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 86 | responder.eapolIn = eapolIn |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 87 | |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 88 | go func() { |
| 89 | logger.Debug("EAPOL response process starts") |
| 90 | defer logger.Debug("EAPOL response process was done") |
| 91 | for { |
| 92 | select { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 93 | case msg := <-eapolOut: |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 94 | logger.Debug("Received eapol from eapolOut intfid:%d onuid:%d", msg.IntfID, msg.OnuID) |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 95 | responder := getEAPResponder() |
| 96 | clients := responder.clients |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 97 | if c, ok := clients[clientKey{intfid: msg.IntfID, onuid: msg.OnuID}]; ok { |
| 98 | logger.Debug("Got client intfid:%d onuid: %d (ClientID: %v)", c.key.intfid, c.key.onuid, c.curID) |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 99 | nextstate := respondMessage("EAPOL", *c, msg, eapolIn) |
| 100 | c.updateState(nextstate) |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 101 | } else { |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 102 | logger.WithFields(log.Fields{ |
| 103 | "clients": clients, |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 104 | }).Errorf("Failed to find eapol client instance intfid:%d onuid:%d", msg.IntfID, msg.OnuID) |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 105 | } |
| 106 | case <-ctx.Done(): |
| 107 | return |
| 108 | } |
| 109 | } |
| 110 | }() |
| 111 | } |
| 112 | |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 113 | func respondMessage(msgtype string, client clientInstance, recvmsg *byteMsg, msgInCh chan *byteMsg) clientState { |
| 114 | curstate := client.getState() |
| 115 | nextstate, sendbytes, err := client.transitState(curstate, recvmsg.Byte) |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 116 | |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 117 | if err != nil { |
| 118 | msg := fmt.Sprintf("Failed to transitState in %s: %s", msgtype, err) |
| 119 | logger.Error(msg, err) |
| 120 | } |
| 121 | |
| 122 | if sendbytes != nil { |
| 123 | key := client.getKey() |
| 124 | if err := sendBytes(key, sendbytes, msgInCh); err != nil { |
| 125 | msg := fmt.Sprintf("Failed to sendBytes in %s: %s", msgtype, err) |
| 126 | logger.Error(msg) |
| 127 | } |
| 128 | } else { |
| 129 | logger.Debug("sendbytes is nil") |
| 130 | } |
| 131 | return nextstate |
| 132 | } |
| 133 | |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 134 | func sendEAPStart(intfid uint32, onuid uint32, client eapClientInstance, bytes []byte, eapolIn chan *byteMsg) error { |
| 135 | for { |
| 136 | responder := getEAPResponder() |
| 137 | clients := responder.clients |
| 138 | if c, ok := clients[clientKey{intfid: intfid, onuid: onuid}]; ok { |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 139 | if c.curState == EapSuccess { |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 140 | logger.WithFields(log.Fields{ |
| 141 | "int_id": intfid, |
| 142 | "onu_id": onuid, |
| 143 | }).Debug("EAP_SUCCESS received, stop retrying") |
| 144 | break |
| 145 | } |
A R Karthick | b3f4ce9 | 2019-08-09 00:16:21 +0000 | [diff] [blame] | 146 | // Reset state to EAP start |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 147 | c.updateState(EapStart) |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 148 | } else { |
| 149 | logger.WithFields(log.Fields{ |
| 150 | "clients": clients, |
| 151 | }).Errorf("Failed to find eapol client instance intfid:%d onuid:%d (sendEAPStart)", intfid, onuid) |
| 152 | } |
| 153 | if err := sendBytes(clientKey{intfid, onuid}, bytes, eapolIn); err != nil { |
| 154 | return errors.New("Failed to send EAPStart") |
| 155 | } |
| 156 | logger.WithFields(log.Fields{ |
Matt Jeanneret | 7c9c5f2 | 2019-08-09 14:40:12 -0400 | [diff] [blame] | 157 | "int_id": intfid, |
| 158 | "onu_id": onuid, |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 159 | "eapolIn": eapolIn, |
Matt Jeanneret | 7c9c5f2 | 2019-08-09 14:40:12 -0400 | [diff] [blame] | 160 | "bytes": bytes, |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 161 | }).Debug("EAPStart Sent") |
| 162 | time.Sleep(30 * time.Second) |
| 163 | } |
| 164 | return nil |
| 165 | } |
| 166 | |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 167 | func startEAPClient(intfid uint32, onuid uint32) error { |
| 168 | client := eapClientInstance{key: clientKey{intfid: intfid, onuid: onuid}, |
| 169 | srcaddr: &net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, 0x07, byte(onuid)}, |
| 170 | version: 1, |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 171 | curID: 0, |
| 172 | curState: EapStart} |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 173 | |
| 174 | eap := client.createEAPStart() |
| 175 | bytes := client.createEAPOL(eap) |
| 176 | resp := getEAPResponder() |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 177 | eapolIn := resp.eapolIn |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 178 | // start a loop that keeps sending EAPOL packets until it succeeds |
| 179 | go sendEAPStart(intfid, onuid, client, bytes, eapolIn) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 180 | // clients[key{intfid: intfid, onuid: onuid}] = &client |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 181 | resp.clients[clientKey{intfid: intfid, onuid: onuid}] = &client |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 182 | return nil |
| 183 | } |
| 184 | |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 185 | func (c eapClientInstance) transitState(cur clientState, recvbytes []byte) (next clientState, respbytes []byte, err error) { |
| 186 | recvpkt := gopacket.NewPacket(recvbytes, layers.LayerTypeEthernet, gopacket.Default) |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 187 | eap, err := extractEAP(recvpkt) |
| 188 | if err != nil { |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 189 | return cur, nil, nil |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 190 | } |
| 191 | if eap.Code == layers.EAPCodeRequest && eap.Type == layers.EAPTypeIdentity { |
| 192 | logger.Debug("Received EAP-Request/Identity") |
| 193 | logger.Debug(recvpkt.Dump()) |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 194 | c.curID = eap.Id |
| 195 | if cur == EapStart { |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 196 | reseap := c.createEAPResID() |
| 197 | pkt := c.createEAPOL(reseap) |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 198 | logger.Debug("Moving from EapStart to EapRespid") |
| 199 | return EapRespid, pkt, nil |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 200 | } |
| 201 | } else if eap.Code == layers.EAPCodeRequest && eap.Type == layers.EAPTypeOTP { |
| 202 | logger.Debug("Received EAP-Request/Challenge") |
| 203 | logger.Debug(recvpkt.Dump()) |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 204 | if cur == EapRespid { |
| 205 | c.curID = eap.Id |
| 206 | senddata := getMD5Data(c.curID, eap) |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 207 | senddata = append([]byte{0x10}, senddata...) |
| 208 | sendeap := c.createEAPResCha(senddata) |
| 209 | pkt := c.createEAPOL(sendeap) |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 210 | logger.Debug("Moving from EapRespid to EapRespcha") |
| 211 | return EapRespcha, pkt, nil |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 212 | } |
| 213 | } else if eap.Code == layers.EAPCodeSuccess && eap.Type == layers.EAPTypeNone { |
| 214 | logger.Debug("Received EAP-Success") |
| 215 | logger.Debug(recvpkt.Dump()) |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 216 | if cur == EapRespcha { |
| 217 | logger.Debug("Moving from EapRespcha to EapSuccess") |
| 218 | return EapSuccess, nil, nil |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 219 | } |
| 220 | } else { |
| 221 | logger.Debug("Received unsupported EAP") |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 222 | return cur, nil, nil |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 223 | } |
| 224 | logger.Debug("State transition does not support..current state:%d", cur) |
| 225 | logger.Debug(recvpkt.Dump()) |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 226 | return cur, nil, nil |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 227 | } |
| 228 | |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 229 | func (c eapClientInstance) getState() clientState { |
| 230 | return c.curState |
| 231 | } |
| 232 | |
| 233 | func (c *eapClientInstance) updateState(state clientState) { |
| 234 | msg := fmt.Sprintf("EAP update state intfid:%d onuid:%d state:%d", c.key.intfid, c.key.onuid, state) |
| 235 | logger.Debug(msg) |
| 236 | c.curState = state |
| 237 | } |
| 238 | |
| 239 | func (c eapClientInstance) getKey() clientKey { |
| 240 | return c.key |
| 241 | } |
| 242 | |
| 243 | func sendBytes(key clientKey, pkt []byte, chIn chan *byteMsg) error { |
| 244 | // Send our packet |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 245 | msg := byteMsg{IntfID: key.intfid, |
| 246 | OnuID: key.onuid, |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 247 | Byte: pkt} |
| 248 | chIn <- &msg |
| 249 | logger.Debug("sendBytes intfid:%d onuid:%d", key.intfid, key.onuid) |
| 250 | logger.Debug(hex.Dump(msg.Byte)) |
| 251 | return nil |
| 252 | } |
| 253 | |
| 254 | func (c *eapClientInstance) createEAPOL(eap *layers.EAP) []byte { |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 255 | buffer := gopacket.NewSerializeBuffer() |
| 256 | options := gopacket.SerializeOptions{} |
| 257 | |
| 258 | ethernetLayer := &layers.Ethernet{ |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 259 | SrcMAC: *c.srcaddr, |
| 260 | DstMAC: net.HardwareAddr{0x01, 0x80, 0xC2, 0x00, 0x00, 0x03}, |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 261 | EthernetType: layers.EthernetTypeEAPOL, |
| 262 | } |
| 263 | |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 264 | if eap == nil { // EAP Start |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 265 | _ = gopacket.SerializeLayers(buffer, options, |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 266 | ethernetLayer, |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 267 | &layers.EAPOL{Version: c.version, Type: 1, Length: 0}, |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 268 | ) |
| 269 | } else { |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 270 | _ = gopacket.SerializeLayers(buffer, options, |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 271 | ethernetLayer, |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 272 | &layers.EAPOL{Version: c.version, Type: 0, Length: eap.Length}, |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 273 | eap, |
| 274 | ) |
| 275 | } |
| 276 | bytes := buffer.Bytes() |
| 277 | return bytes |
| 278 | } |
| 279 | |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 280 | func (c *eapClientInstance) createEAPStart() *layers.EAP { |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 281 | return nil |
| 282 | } |
| 283 | |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 284 | func (c *eapClientInstance) createEAPResID() *layers.EAP { |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 285 | eap := layers.EAP{Code: layers.EAPCodeResponse, |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 286 | Id: c.curID, |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 287 | Length: 9, |
| 288 | Type: layers.EAPTypeIdentity, |
| 289 | TypeData: []byte{0x75, 0x73, 0x65, 0x72}} |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 290 | return &eap |
| 291 | } |
| 292 | |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 293 | func (c *eapClientInstance) createEAPResCha(payload []byte) *layers.EAP { |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 294 | eap := layers.EAP{Code: layers.EAPCodeResponse, |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 295 | Id: c.curID, Length: 22, |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 296 | Type: layers.EAPTypeOTP, |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 297 | TypeData: payload} |
| 298 | return &eap |
| 299 | } |
| 300 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 301 | func getMD5Data(id uint8, eap *layers.EAP) []byte { |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 302 | i := byte(id) |
| 303 | C := []byte(eap.BaseLayer.Contents)[6:] |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 304 | P := []byte{i, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64} // "password" |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 305 | data := md5.Sum(append(P, C...)) |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 306 | ret := make([]byte, 16) |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 307 | for j := 0; j < 16; j++ { |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 308 | ret[j] = data[j] |
| 309 | } |
| 310 | return ret |
| 311 | } |
| 312 | |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 313 | func extractEAPOL(pkt gopacket.Packet) (*layers.EAPOL, error) { |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 314 | layerEAPOL := pkt.Layer(layers.LayerTypeEAPOL) |
| 315 | eapol, _ := layerEAPOL.(*layers.EAPOL) |
| 316 | if eapol == nil { |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 317 | return nil, errors.New("cannot extract EAPOL") |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 318 | } |
| 319 | return eapol, nil |
| 320 | } |
| 321 | |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 322 | func extractEAP(pkt gopacket.Packet) (*layers.EAP, error) { |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 323 | layerEAP := pkt.Layer(layers.LayerTypeEAP) |
| 324 | eap, _ := layerEAP.(*layers.EAP) |
| 325 | if eap == nil { |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame^] | 326 | return nil, errors.New("cannot extract EAP") |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 327 | } |
| 328 | return eap, nil |
Keita NISHIMOTO | dad44cb | 2019-02-08 09:45:40 +0900 | [diff] [blame] | 329 | } |