blob: ac11dcc024e0dea40a63e5ae9e8444999ed61bb9 [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
19package org.onosproject.aaa.packet;
20
21import org.onlab.packet.BasePacket;
alshabib7b808c52015-06-26 14:22:24 -070022import org.onlab.packet.Deserializer;
Ari Saha79d7c252015-06-26 10:31:48 -070023import org.onlab.packet.Ethernet;
24import org.onlab.packet.IPacket;
25import org.onlab.packet.MacAddress;
26
27import java.nio.ByteBuffer;
28
alshabib7b808c52015-06-26 14:22:24 -070029import static org.onlab.packet.PacketUtils.checkInput;
30
Ari Saha79d7c252015-06-26 10:31:48 -070031/**
32 *
33 */
34public class EAPOL extends BasePacket {
35
36 private byte version = 0x01;
37 private byte eapolType;
38 private short packetLength;
39
40 /* EAPOL Packet Type */
41 public static final byte EAPOL_PACKET = 0x0;
42 public static final byte EAPOL_START = 0x1;
43 public static final byte EAPOL_LOGOFF = 0x2;
44 public static final byte EAPOL_KEY = 0x3;
45 public static final byte EAPOL_ASF = 0x4;
46
47 public static final MacAddress PAE_GROUP_ADDR = MacAddress.valueOf(new byte[] {
48 (byte) 0x01, (byte) 0x80, (byte) 0xc2, (byte) 0x00, (byte) 0x00, (byte) 0x03
49 });
50
51
52 /**
53 * Get version.
54 * @return version
55 */
56 public byte getVersion() {
57 return this.version;
58 }
59
60 /**
61 * Set version.
62 * @param version EAPOL version
63 * @return this
64 */
65 public EAPOL setVersion(final byte version) {
66 this.version = version;
67 return this;
68 }
69
70 /**
71 * Get type.
72 * @return EAPOL type
73 */
74 public byte getEapolType() {
75 return this.eapolType;
76 }
77
78 /**
79 * Set EAPOL type.
80 * @param eapolType EAPOL type
81 * @return this
82 */
83 public EAPOL setEapolType(final byte eapolType) {
84 this.eapolType = eapolType;
85 return this;
86 }
87
88 /**
89 * Get packet length.
90 * @return packet length
91 */
92 public short getPacketLength() {
93 return this.packetLength;
94 }
95
96 /**
97 * Set packet length.
98 * @param packetLen packet length
99 * @return this
100 */
101 public EAPOL setPacketLength(final short packetLen) {
102 this.packetLength = packetLen;
103 return this;
104 }
105
106
107
108 /**
109 * Serializes the packet, based on the code/type using the payload
110 * to compute its length.
111 * @return this
112 */
113 @Override
114 public byte[] serialize() {
115
116 byte[] payloadData = null;
117
118 if (this.payload != null) {
119 this.payload.setParent(this);
120 payloadData = this.payload.serialize();
121 }
122
123 //prepare the buffer to hold the version (1), packet type (1), packet length (2) and the eap payload.
124 //if there is no payload, packet length is 0
125 byte[] data = new byte[4 + this.packetLength];
126 final ByteBuffer bb = ByteBuffer.wrap(data);
127 bb.put(this.version);
128 bb.put(this.eapolType);
129 bb.putShort(this.packetLength);
130
131 //put the EAP payload
132 if (payloadData != null) {
133 bb.put(payloadData);
134 }
135
136 return data;
137 }
138
Ari Saha79d7c252015-06-26 10:31:48 -0700139
140
Ari Saha79d7c252015-06-26 10:31:48 -0700141 @Override
142 public int hashCode() {
143 final int prime = 3889;
144 int result = super.hashCode();
145 result = prime * result + this.version;
146 result = prime * result + this.eapolType;
147 result = prime * result + this.packetLength;
148 return result;
149 }
150
151 /**
152 *
153 * @param dstMac
154 * @param srcMac
155 * @param eapolType
156 * @param eap
157 * @return Ethernet frame
158 */
159 public static Ethernet buildEapolResponse(MacAddress dstMac, MacAddress srcMac,
160 short vlan, byte eapolType, EAP eap) {
161
162 Ethernet eth = new Ethernet();
163 eth.setDestinationMACAddress(dstMac.toBytes());
164 eth.setSourceMACAddress(srcMac.toBytes());
165 eth.setEtherType(EAPEthernet.TYPE_PAE);
166 if (vlan != Ethernet.VLAN_UNTAGGED) {
167 eth.setVlanID(vlan);
168 }
169 //eapol header
170 EAPOL eapol = new EAPOL();
171 eapol.setEapolType(eapolType);
172 eapol.setPacketLength(eap.getLength());
173
174 //eap part
175 eapol.setPayload(eap);
176
177 eth.setPayload(eapol);
178 eth.setPad(true);
179 return eth;
180 }
alshabib7b808c52015-06-26 14:22:24 -0700181
182 public static Deserializer<EAPOL> deserializer() {
183 return (data, offset, length) -> {
184 checkInput(data, offset, length, 0);
185
186 EAPOL eapol = new EAPOL();
187 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
188 eapol.setVersion(bb.get());
189 eapol.setEapolType(bb.get());
190 eapol.setPacketLength(bb.getShort());
191
192 if (eapol.packetLength > 0) {
193 //deserialize the EAP Payload
194 eapol.payload = new EAP();
195
196 eapol.payload = eapol.payload.deserialize(data, bb.position(), length - 4);
197 eapol.payload.setParent(eapol);
198 }
199 return eapol;
200 };
201 }
202
203 @Override
204 public IPacket deserialize(final byte[] data, final int offset,
205 final int length) {
206 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
207
208
209 //deserialize the EAPOL header
210 this.version = bb.get();
211 this.eapolType = bb.get();
212 this.packetLength = bb.getShort();
213
214 if (this.packetLength > 0) {
215 //deserialize the EAP Payload
216 this.payload = new EAP();
217
218 this.payload = this.payload.deserialize(data, bb.position(), length - 4);
219 this.payload.setParent(this);
220 }
221
222
223 return this;
224 }
225
226
Ari Saha79d7c252015-06-26 10:31:48 -0700227}
228