Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * * Copyright 2015 AT&T Foundry |
| 4 | * * |
| 5 | * * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * * you may not use this file except in compliance with the License. |
| 7 | * * You may obtain a copy of the License at |
| 8 | * * |
| 9 | * * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * * |
| 11 | * * Unless required by applicable law or agreed to in writing, software |
| 12 | * * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * * See the License for the specific language governing permissions and |
| 15 | * * limitations under the License. |
| 16 | * |
| 17 | */ |
| 18 | |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 19 | package org.onlab.packet; |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 20 | |
| 21 | import java.nio.ByteBuffer; |
| 22 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 23 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 24 | import static org.onlab.packet.PacketUtils.checkHeaderLength; |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 25 | import static org.onlab.packet.PacketUtils.checkInput; |
| 26 | |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 27 | /** |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 28 | * EAPOL (Extensible Authentication Protocol over LAN) header. |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 29 | */ |
| 30 | public class EAPOL extends BasePacket { |
| 31 | |
Anjali K K | 4a694f6 | 2018-07-12 19:09:19 +0530 | [diff] [blame] | 32 | // private byte version = 0x01; |
| 33 | private byte version = 0x03; |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 34 | private byte eapolType; |
| 35 | private short packetLength; |
| 36 | |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 37 | private static final int HEADER_LENGTH = 4; |
| 38 | |
| 39 | // EAPOL Packet Type |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 40 | public static final byte EAPOL_PACKET = 0x0; |
Anjali K K | 4a694f6 | 2018-07-12 19:09:19 +0530 | [diff] [blame] | 41 | public static final byte EAPOL_START = 0x1; |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 42 | public static final byte EAPOL_LOGOFF = 0x2; |
Anjali K K | 4a694f6 | 2018-07-12 19:09:19 +0530 | [diff] [blame] | 43 | public static final byte EAPOL_KEY = 0x3; |
| 44 | public static final byte EAPOL_ASF = 0x4; |
| 45 | public static final byte EAPOL_MKA = 0X5; |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 46 | |
Anjali K K | 4a694f6 | 2018-07-12 19:09:19 +0530 | [diff] [blame] | 47 | public static final MacAddress PAE_GROUP_ADDR = MacAddress.valueOf(new byte[]{ |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 48 | (byte) 0x01, (byte) 0x80, (byte) 0xc2, (byte) 0x00, (byte) 0x00, (byte) 0x03 |
| 49 | }); |
| 50 | |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 51 | /** |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 52 | * Gets the version. |
| 53 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 54 | * @return version |
| 55 | */ |
| 56 | public byte getVersion() { |
| 57 | return this.version; |
| 58 | } |
| 59 | |
| 60 | /** |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 61 | * Sets the version. |
| 62 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 63 | * @param version EAPOL version |
| 64 | * @return this |
| 65 | */ |
| 66 | public EAPOL setVersion(final byte version) { |
| 67 | this.version = version; |
| 68 | return this; |
| 69 | } |
| 70 | |
| 71 | /** |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 72 | * Gets the type. |
| 73 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 74 | * @return EAPOL type |
| 75 | */ |
| 76 | public byte getEapolType() { |
| 77 | return this.eapolType; |
| 78 | } |
| 79 | |
| 80 | /** |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 81 | * Sets the EAPOL type. |
| 82 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 83 | * @param eapolType EAPOL type |
| 84 | * @return this |
| 85 | */ |
| 86 | public EAPOL setEapolType(final byte eapolType) { |
| 87 | this.eapolType = eapolType; |
| 88 | return this; |
| 89 | } |
| 90 | |
| 91 | /** |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 92 | * Gets the packet length. |
| 93 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 94 | * @return packet length |
| 95 | */ |
| 96 | public short getPacketLength() { |
| 97 | return this.packetLength; |
| 98 | } |
| 99 | |
| 100 | /** |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 101 | * Sets the packet length. |
| 102 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 103 | * @param packetLen packet length |
| 104 | * @return this |
| 105 | */ |
| 106 | public EAPOL setPacketLength(final short packetLen) { |
| 107 | this.packetLength = packetLen; |
| 108 | return this; |
| 109 | } |
| 110 | |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 111 | /** |
| 112 | * Serializes the packet, based on the code/type using the payload |
| 113 | * to compute its length. |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 114 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 115 | * @return this |
| 116 | */ |
| 117 | @Override |
| 118 | public byte[] serialize() { |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 119 | byte[] payloadData = null; |
| 120 | |
| 121 | if (this.payload != null) { |
| 122 | this.payload.setParent(this); |
| 123 | payloadData = this.payload.serialize(); |
| 124 | } |
| 125 | |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 126 | // prepare the buffer to hold the version (1), packet type (1), |
| 127 | // packet length (2) and the eap payload. |
| 128 | // if there is no payload, packet length is 0 |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 129 | byte[] data = new byte[4 + this.packetLength]; |
| 130 | final ByteBuffer bb = ByteBuffer.wrap(data); |
| 131 | bb.put(this.version); |
| 132 | bb.put(this.eapolType); |
| 133 | bb.putShort(this.packetLength); |
| 134 | |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 135 | // put the EAP payload |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 136 | if (payloadData != null) { |
| 137 | bb.put(payloadData); |
| 138 | } |
| 139 | |
| 140 | return data; |
| 141 | } |
| 142 | |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 143 | @Override |
| 144 | public int hashCode() { |
| 145 | final int prime = 3889; |
| 146 | int result = super.hashCode(); |
| 147 | result = prime * result + this.version; |
| 148 | result = prime * result + this.eapolType; |
| 149 | result = prime * result + this.packetLength; |
| 150 | return result; |
| 151 | } |
| 152 | |
Ray Milkey | 86f20cc | 2015-12-09 16:54:09 -0800 | [diff] [blame] | 153 | @Override |
| 154 | public boolean equals(Object o) { |
| 155 | if (this == o) { |
| 156 | return true; |
| 157 | } |
| 158 | if (!(o instanceof EAPOL)) { |
| 159 | return false; |
| 160 | } |
| 161 | EAPOL that = (EAPOL) o; |
| 162 | |
| 163 | if (this.version != that.version) { |
| 164 | return false; |
| 165 | } |
| 166 | if (this.eapolType != that.eapolType) { |
| 167 | return false; |
| 168 | } |
| 169 | if (this.packetLength != that.packetLength) { |
| 170 | return false; |
| 171 | } |
| 172 | return true; |
| 173 | } |
| 174 | |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 175 | /** |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 176 | * Deserializer for EAPOL packets. |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 177 | * |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 178 | * @return deserializer |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 179 | */ |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 180 | public static Deserializer<EAPOL> deserializer() { |
| 181 | return (data, offset, length) -> { |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 182 | checkInput(data, offset, length, HEADER_LENGTH); |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 183 | |
| 184 | EAPOL eapol = new EAPOL(); |
| 185 | final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 186 | eapol.setVersion(bb.get()); |
| 187 | eapol.setEapolType(bb.get()); |
| 188 | eapol.setPacketLength(bb.getShort()); |
| 189 | |
| 190 | if (eapol.packetLength > 0) { |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 191 | checkHeaderLength(length, HEADER_LENGTH + eapol.packetLength); |
Anjali K K | 4a694f6 | 2018-07-12 19:09:19 +0530 | [diff] [blame] | 192 | if (eapol.getEapolType() == EAPOL_MKA) { |
| 193 | eapol.payload = EAPOLMkpdu.deserializer().deserialize(data, |
| 194 | bb.position(), bb.limit() - bb.position()); |
| 195 | } else { |
| 196 | // deserialize the EAP Payload |
| 197 | eapol.payload = EAP.deserializer().deserialize(data, |
| 198 | bb.position(), bb.limit() - bb.position()); |
| 199 | } |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 200 | eapol.payload.setParent(eapol); |
| 201 | } |
| 202 | return eapol; |
| 203 | }; |
| 204 | } |
| 205 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 206 | |
| 207 | @Override |
| 208 | public String toString() { |
| 209 | return toStringHelper(getClass()) |
| 210 | .add("version", Byte.toString(version)) |
| 211 | .add("eapolType", Byte.toString(eapolType)) |
| 212 | .add("packetLength", Short.toString(packetLength)) |
| 213 | .toString(); |
| 214 | } |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 215 | } |
| 216 | |