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