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