blob: 3d7d7c418a45c1373d622ff98a6cca5e7bda933d [file] [log] [blame]
Ari Saha79d7c252015-06-26 10:31:48 -07001/*
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 Hart4a60bb32015-06-30 15:31:20 -070019package org.onlab.packet;
Ari Saha79d7c252015-06-26 10:31:48 -070020
21import java.nio.ByteBuffer;
22
Jian Li5fc14292015-12-04 11:30:46 -080023import static com.google.common.base.MoreObjects.toStringHelper;
Jonathan Hart4a60bb32015-06-30 15:31:20 -070024import static org.onlab.packet.PacketUtils.checkHeaderLength;
alshabib7b808c52015-06-26 14:22:24 -070025import static org.onlab.packet.PacketUtils.checkInput;
26
Ari Saha79d7c252015-06-26 10:31:48 -070027/**
Jonathan Hart4a60bb32015-06-30 15:31:20 -070028 * EAPOL (Extensible Authentication Protocol over LAN) header.
Ari Saha79d7c252015-06-26 10:31:48 -070029 */
30public class EAPOL extends BasePacket {
31
Anjali K K4a694f62018-07-12 19:09:19 +053032 // private byte version = 0x01;
33 private byte version = 0x03;
Ari Saha79d7c252015-06-26 10:31:48 -070034 private byte eapolType;
35 private short packetLength;
36
Jonathan Hart4a60bb32015-06-30 15:31:20 -070037 private static final int HEADER_LENGTH = 4;
38
39 // EAPOL Packet Type
Ari Saha79d7c252015-06-26 10:31:48 -070040 public static final byte EAPOL_PACKET = 0x0;
Anjali K K4a694f62018-07-12 19:09:19 +053041 public static final byte EAPOL_START = 0x1;
Ari Saha79d7c252015-06-26 10:31:48 -070042 public static final byte EAPOL_LOGOFF = 0x2;
Anjali K K4a694f62018-07-12 19:09:19 +053043 public static final byte EAPOL_KEY = 0x3;
44 public static final byte EAPOL_ASF = 0x4;
45 public static final byte EAPOL_MKA = 0X5;
Ari Saha79d7c252015-06-26 10:31:48 -070046
Anjali K K4a694f62018-07-12 19:09:19 +053047 public static final MacAddress PAE_GROUP_ADDR = MacAddress.valueOf(new byte[]{
Ari Saha79d7c252015-06-26 10:31:48 -070048 (byte) 0x01, (byte) 0x80, (byte) 0xc2, (byte) 0x00, (byte) 0x00, (byte) 0x03
49 });
50
Ari Saha79d7c252015-06-26 10:31:48 -070051 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -070052 * Gets the version.
53 *
Ari Saha79d7c252015-06-26 10:31:48 -070054 * @return version
55 */
56 public byte getVersion() {
57 return this.version;
58 }
59
60 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -070061 * Sets the version.
62 *
Ari Saha79d7c252015-06-26 10:31:48 -070063 * @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 Hart4a60bb32015-06-30 15:31:20 -070072 * Gets the type.
73 *
Ari Saha79d7c252015-06-26 10:31:48 -070074 * @return EAPOL type
75 */
76 public byte getEapolType() {
77 return this.eapolType;
78 }
79
80 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -070081 * Sets the EAPOL type.
82 *
Ari Saha79d7c252015-06-26 10:31:48 -070083 * @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 Hart4a60bb32015-06-30 15:31:20 -070092 * Gets the packet length.
93 *
Ari Saha79d7c252015-06-26 10:31:48 -070094 * @return packet length
95 */
96 public short getPacketLength() {
97 return this.packetLength;
98 }
99
100 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700101 * Sets the packet length.
102 *
Ari Saha79d7c252015-06-26 10:31:48 -0700103 * @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 Saha79d7c252015-06-26 10:31:48 -0700111 /**
112 * Serializes the packet, based on the code/type using the payload
113 * to compute its length.
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700114 *
Ari Saha79d7c252015-06-26 10:31:48 -0700115 * @return this
116 */
117 @Override
118 public byte[] serialize() {
Ari Saha79d7c252015-06-26 10:31:48 -0700119 byte[] payloadData = null;
120
121 if (this.payload != null) {
122 this.payload.setParent(this);
123 payloadData = this.payload.serialize();
124 }
125
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700126 // 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 Saha79d7c252015-06-26 10:31:48 -0700129 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 Hart4a60bb32015-06-30 15:31:20 -0700135 // put the EAP payload
Ari Saha79d7c252015-06-26 10:31:48 -0700136 if (payloadData != null) {
137 bb.put(payloadData);
138 }
139
140 return data;
141 }
142
Ari Saha79d7c252015-06-26 10:31:48 -0700143 @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 Milkey86f20cc2015-12-09 16:54:09 -0800153 @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 Saha79d7c252015-06-26 10:31:48 -0700175 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700176 * Deserializer for EAPOL packets.
Ari Saha79d7c252015-06-26 10:31:48 -0700177 *
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700178 * @return deserializer
Ari Saha79d7c252015-06-26 10:31:48 -0700179 */
alshabib7b808c52015-06-26 14:22:24 -0700180 public static Deserializer<EAPOL> deserializer() {
181 return (data, offset, length) -> {
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700182 checkInput(data, offset, length, HEADER_LENGTH);
alshabib7b808c52015-06-26 14:22:24 -0700183
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 Hart4a60bb32015-06-30 15:31:20 -0700191 checkHeaderLength(length, HEADER_LENGTH + eapol.packetLength);
Anjali K K4a694f62018-07-12 19:09:19 +0530192 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 }
alshabib7b808c52015-06-26 14:22:24 -0700200 eapol.payload.setParent(eapol);
201 }
202 return eapol;
203 };
204 }
205
Jian Li5fc14292015-12-04 11:30:46 -0800206
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 Saha79d7c252015-06-26 10:31:48 -0700215}
216