Jonathan Hart
Committed by Gerrit Code Review

Cleaned up AAA app now it's in the ONOS core.

Moved packets into the packet library, minor app cleanups and javadoc.

Change-Id: I7ee04d09f82051fdb2a9bcfe577cb163661d5055
1 -/*
2 - * Copyright 2015 Open Networking Laboratory
3 - *
4 - * Licensed under the Apache License, Version 2.0 (the "License");
5 - * you may not use this file except in compliance with the License.
6 - * You may obtain a copy of the License at
7 - *
8 - * http://www.apache.org/licenses/LICENSE-2.0
9 - *
10 - * Unless required by applicable law or agreed to in writing, software
11 - * distributed under the License is distributed on an "AS IS" BASIS,
12 - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 - * See the License for the specific language governing permissions and
14 - * limitations under the License.
15 - */
16 -
17 -package org.onosproject.aaa.packet;
18 -
19 -import org.onlab.packet.Deserializer;
20 -import org.onlab.packet.EthType;
21 -import org.onlab.packet.Ethernet;
22 -import org.onlab.packet.IPacket;
23 -
24 -import java.util.HashMap;
25 -import java.util.Map;
26 -
27 -/**
28 - * Created by jono on 5/19/15.
29 - */
30 -public final class EAPEthernet extends Ethernet {
31 -
32 - public static final short TYPE_PAE = (short) 0x888e;
33 -
34 - private static final Map<Short, Deserializer<? extends IPacket>> ETHERTYPE_DESERIALIZER_MAP =
35 - new HashMap<>();
36 -
37 - private EAPEthernet() {
38 -
39 - }
40 -
41 - static {
42 - for (EthType.EtherType ethType : EthType.EtherType.values()) {
43 - if (ethType.deserializer() != null) {
44 - ETHERTYPE_DESERIALIZER_MAP.put(ethType.ethType().toShort(),
45 - ethType.deserializer());
46 - }
47 - }
48 - ETHERTYPE_DESERIALIZER_MAP.put((short) 0x888e, EAPOL.deserializer());
49 - }
50 -
51 -}
...@@ -16,29 +16,30 @@ ...@@ -16,29 +16,30 @@
16 * 16 *
17 */ 17 */
18 18
19 -package org.onosproject.aaa.packet; 19 +package org.onlab.packet;
20 -
21 -import org.onlab.packet.BasePacket;
22 -import org.onlab.packet.IPacket;
23 20
24 import java.nio.ByteBuffer; 21 import java.nio.ByteBuffer;
25 22
23 +import static org.onlab.packet.PacketUtils.checkHeaderLength;
24 +import static org.onlab.packet.PacketUtils.checkInput;
26 25
27 /** 26 /**
28 - * 27 + * EAP (Extensible Authentication Protocol) packet.
29 */ 28 */
30 public class EAP extends BasePacket { 29 public class EAP extends BasePacket {
30 + private static final int HEADER_LENGTH = 4;
31 +
31 public static final short MIN_LEN = 0x4; 32 public static final short MIN_LEN = 0x4;
32 public static final short EAP_HDR_LEN_REQ_RESP = 5; 33 public static final short EAP_HDR_LEN_REQ_RESP = 5;
33 public static final short EAP_HDR_LEN_SUC_FAIL = 4; 34 public static final short EAP_HDR_LEN_SUC_FAIL = 4;
34 35
35 - /* EAP Code */ 36 + // EAP Code
36 public static final byte REQUEST = 0x1; 37 public static final byte REQUEST = 0x1;
37 public static final byte RESPONSE = 0x2; 38 public static final byte RESPONSE = 0x2;
38 public static final byte SUCCESS = 0x3; 39 public static final byte SUCCESS = 0x3;
39 public static final byte FAILURE = 0x4; 40 public static final byte FAILURE = 0x4;
40 41
41 - /* EAP Attribute Type */ 42 + // EAP Attribute Type
42 public static final byte ATTR_IDENTITY = 0x1; 43 public static final byte ATTR_IDENTITY = 0x1;
43 public static final byte ATTR_NOTIFICATION = 0x2; 44 public static final byte ATTR_NOTIFICATION = 0x2;
44 public static final byte ATTR_NAK = 0x3; 45 public static final byte ATTR_NAK = 0x3;
...@@ -55,7 +56,8 @@ public class EAP extends BasePacket { ...@@ -55,7 +56,8 @@ public class EAP extends BasePacket {
55 56
56 57
57 /** 58 /**
58 - * Get the EAP code. 59 + * Gets the EAP code.
60 + *
59 * @return EAP code 61 * @return EAP code
60 */ 62 */
61 public byte getCode() { 63 public byte getCode() {
...@@ -64,7 +66,8 @@ public class EAP extends BasePacket { ...@@ -64,7 +66,8 @@ public class EAP extends BasePacket {
64 66
65 67
66 /** 68 /**
67 - * Set the EAP code. 69 + * Sets the EAP code.
70 + *
68 * @param code EAP code 71 * @param code EAP code
69 * @return this 72 * @return this
70 */ 73 */
...@@ -74,7 +77,8 @@ public class EAP extends BasePacket { ...@@ -74,7 +77,8 @@ public class EAP extends BasePacket {
74 } 77 }
75 78
76 /** 79 /**
77 - * Get the EAP identifier. 80 + * Gets the EAP identifier.
81 + *
78 * @return EAP identifier 82 * @return EAP identifier
79 */ 83 */
80 public byte getIdentifier() { 84 public byte getIdentifier() {
...@@ -82,7 +86,8 @@ public class EAP extends BasePacket { ...@@ -82,7 +86,8 @@ public class EAP extends BasePacket {
82 } 86 }
83 87
84 /** 88 /**
85 - * Set the EAP identifier. 89 + * Sets the EAP identifier.
90 + *
86 * @param identifier 91 * @param identifier
87 * @return this 92 * @return this
88 */ 93 */
...@@ -92,7 +97,8 @@ public class EAP extends BasePacket { ...@@ -92,7 +97,8 @@ public class EAP extends BasePacket {
92 } 97 }
93 98
94 /** 99 /**
95 - * Get the get packet length. 100 + * Gets the get packet length.
101 + *
96 * @return packet length 102 * @return packet length
97 */ 103 */
98 public short getLength() { 104 public short getLength() {
...@@ -100,7 +106,8 @@ public class EAP extends BasePacket { ...@@ -100,7 +106,8 @@ public class EAP extends BasePacket {
100 } 106 }
101 107
102 /** 108 /**
103 - * Set the packet length. 109 + * Sets the packet length.
110 + *
104 * @param length packet length 111 * @param length packet length
105 * @return this 112 * @return this
106 */ 113 */
...@@ -110,7 +117,8 @@ public class EAP extends BasePacket { ...@@ -110,7 +117,8 @@ public class EAP extends BasePacket {
110 } 117 }
111 118
112 /** 119 /**
113 - * Get the data type. 120 + * Gets the data type.
121 + *
114 * @return data type 122 * @return data type
115 */ 123 */
116 public byte getDataType() { 124 public byte getDataType() {
...@@ -118,7 +126,8 @@ public class EAP extends BasePacket { ...@@ -118,7 +126,8 @@ public class EAP extends BasePacket {
118 } 126 }
119 127
120 /** 128 /**
121 - * Set the data type. 129 + * Sets the data type.
130 + *
122 * @param type data type 131 * @param type data type
123 * @return this 132 * @return this
124 */ 133 */
...@@ -128,7 +137,8 @@ public class EAP extends BasePacket { ...@@ -128,7 +137,8 @@ public class EAP extends BasePacket {
128 } 137 }
129 138
130 /** 139 /**
131 - * Get the EAP data. 140 + * Gets the EAP data.
141 + *
132 * @return EAP data 142 * @return EAP data
133 */ 143 */
134 public byte[] getData() { 144 public byte[] getData() {
...@@ -136,7 +146,8 @@ public class EAP extends BasePacket { ...@@ -136,7 +146,8 @@ public class EAP extends BasePacket {
136 } 146 }
137 147
138 /** 148 /**
139 - * Set the EAP data. 149 + * Sets the EAP data.
150 + *
140 * @param data EAP data to be set 151 * @param data EAP data to be set
141 * @return this 152 * @return this
142 */ 153 */
...@@ -146,7 +157,7 @@ public class EAP extends BasePacket { ...@@ -146,7 +157,7 @@ public class EAP extends BasePacket {
146 } 157 }
147 158
148 /** 159 /**
149 - * Default EAP constructor that set the EAP code to 0. 160 + * Default EAP constructor that sets the EAP code to 0.
150 */ 161 */
151 public EAP() { 162 public EAP() {
152 this.code = 0; 163 this.code = 0;
...@@ -154,6 +165,7 @@ public class EAP extends BasePacket { ...@@ -154,6 +165,7 @@ public class EAP extends BasePacket {
154 165
155 /** 166 /**
156 * EAP constructor that initially sets all fields. 167 * EAP constructor that initially sets all fields.
168 + *
157 * @param code EAP code 169 * @param code EAP code
158 * @param identifier EAP identifier 170 * @param identifier EAP identifier
159 * @param type packet type 171 * @param type packet type
...@@ -172,10 +184,36 @@ public class EAP extends BasePacket { ...@@ -172,10 +184,36 @@ public class EAP extends BasePacket {
172 } 184 }
173 185
174 /** 186 /**
175 - * Serializes the packet, based on the code/type using the payload 187 + * Deserializer for EAP packets.
176 - * to compute its length. 188 + *
177 - * @return the serialized payload 189 + * @return deserializer
178 */ 190 */
191 + public static Deserializer<EAP> deserializer() {
192 + return (data, offset, length) -> {
193 + checkInput(data, offset, length, HEADER_LENGTH);
194 +
195 + EAP eap = new EAP();
196 + final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
197 + eap.code = bb.get();
198 + eap.identifier = bb.get();
199 + eap.length = bb.getShort();
200 +
201 + checkHeaderLength(length, HEADER_LENGTH + eap.length);
202 +
203 + int dataLength;
204 + if (eap.code == REQUEST || eap.code == RESPONSE) {
205 + eap.type = bb.get();
206 + dataLength = eap.length - 5;
207 + } else {
208 + dataLength = eap.length - 4;
209 + }
210 +
211 + eap.data = new byte[dataLength];
212 + bb.get(eap.data);
213 + return eap;
214 + };
215 + }
216 +
179 @Override 217 @Override
180 public byte[] serialize() { 218 public byte[] serialize() {
181 final byte[] data = new byte[this.length]; 219 final byte[] data = new byte[this.length];
......
...@@ -16,20 +16,15 @@ ...@@ -16,20 +16,15 @@
16 * 16 *
17 */ 17 */
18 18
19 -package org.onosproject.aaa.packet; 19 +package org.onlab.packet;
20 -
21 -import org.onlab.packet.BasePacket;
22 -import org.onlab.packet.Deserializer;
23 -import org.onlab.packet.Ethernet;
24 -import org.onlab.packet.IPacket;
25 -import org.onlab.packet.MacAddress;
26 20
27 import java.nio.ByteBuffer; 21 import java.nio.ByteBuffer;
28 22
23 +import static org.onlab.packet.PacketUtils.checkHeaderLength;
29 import static org.onlab.packet.PacketUtils.checkInput; 24 import static org.onlab.packet.PacketUtils.checkInput;
30 25
31 /** 26 /**
32 - * 27 + * EAPOL (Extensible Authentication Protocol over LAN) header.
33 */ 28 */
34 public class EAPOL extends BasePacket { 29 public class EAPOL extends BasePacket {
35 30
...@@ -37,7 +32,9 @@ public class EAPOL extends BasePacket { ...@@ -37,7 +32,9 @@ public class EAPOL extends BasePacket {
37 private byte eapolType; 32 private byte eapolType;
38 private short packetLength; 33 private short packetLength;
39 34
40 - /* EAPOL Packet Type */ 35 + private static final int HEADER_LENGTH = 4;
36 +
37 + // EAPOL Packet Type
41 public static final byte EAPOL_PACKET = 0x0; 38 public static final byte EAPOL_PACKET = 0x0;
42 public static final byte EAPOL_START = 0x1; 39 public static final byte EAPOL_START = 0x1;
43 public static final byte EAPOL_LOGOFF = 0x2; 40 public static final byte EAPOL_LOGOFF = 0x2;
...@@ -48,9 +45,9 @@ public class EAPOL extends BasePacket { ...@@ -48,9 +45,9 @@ public class EAPOL extends BasePacket {
48 (byte) 0x01, (byte) 0x80, (byte) 0xc2, (byte) 0x00, (byte) 0x00, (byte) 0x03 45 (byte) 0x01, (byte) 0x80, (byte) 0xc2, (byte) 0x00, (byte) 0x00, (byte) 0x03
49 }); 46 });
50 47
51 -
52 /** 48 /**
53 - * Get version. 49 + * Gets the version.
50 + *
54 * @return version 51 * @return version
55 */ 52 */
56 public byte getVersion() { 53 public byte getVersion() {
...@@ -58,7 +55,8 @@ public class EAPOL extends BasePacket { ...@@ -58,7 +55,8 @@ public class EAPOL extends BasePacket {
58 } 55 }
59 56
60 /** 57 /**
61 - * Set version. 58 + * Sets the version.
59 + *
62 * @param version EAPOL version 60 * @param version EAPOL version
63 * @return this 61 * @return this
64 */ 62 */
...@@ -68,7 +66,8 @@ public class EAPOL extends BasePacket { ...@@ -68,7 +66,8 @@ public class EAPOL extends BasePacket {
68 } 66 }
69 67
70 /** 68 /**
71 - * Get type. 69 + * Gets the type.
70 + *
72 * @return EAPOL type 71 * @return EAPOL type
73 */ 72 */
74 public byte getEapolType() { 73 public byte getEapolType() {
...@@ -76,7 +75,8 @@ public class EAPOL extends BasePacket { ...@@ -76,7 +75,8 @@ public class EAPOL extends BasePacket {
76 } 75 }
77 76
78 /** 77 /**
79 - * Set EAPOL type. 78 + * Sets the EAPOL type.
79 + *
80 * @param eapolType EAPOL type 80 * @param eapolType EAPOL type
81 * @return this 81 * @return this
82 */ 82 */
...@@ -86,7 +86,8 @@ public class EAPOL extends BasePacket { ...@@ -86,7 +86,8 @@ public class EAPOL extends BasePacket {
86 } 86 }
87 87
88 /** 88 /**
89 - * Get packet length. 89 + * Gets the packet length.
90 + *
90 * @return packet length 91 * @return packet length
91 */ 92 */
92 public short getPacketLength() { 93 public short getPacketLength() {
...@@ -94,7 +95,8 @@ public class EAPOL extends BasePacket { ...@@ -94,7 +95,8 @@ public class EAPOL extends BasePacket {
94 } 95 }
95 96
96 /** 97 /**
97 - * Set packet length. 98 + * Sets the packet length.
99 + *
98 * @param packetLen packet length 100 * @param packetLen packet length
99 * @return this 101 * @return this
100 */ 102 */
...@@ -103,16 +105,14 @@ public class EAPOL extends BasePacket { ...@@ -103,16 +105,14 @@ public class EAPOL extends BasePacket {
103 return this; 105 return this;
104 } 106 }
105 107
106 -
107 -
108 /** 108 /**
109 * Serializes the packet, based on the code/type using the payload 109 * Serializes the packet, based on the code/type using the payload
110 * to compute its length. 110 * to compute its length.
111 + *
111 * @return this 112 * @return this
112 */ 113 */
113 @Override 114 @Override
114 public byte[] serialize() { 115 public byte[] serialize() {
115 -
116 byte[] payloadData = null; 116 byte[] payloadData = null;
117 117
118 if (this.payload != null) { 118 if (this.payload != null) {
...@@ -120,15 +120,16 @@ public class EAPOL extends BasePacket { ...@@ -120,15 +120,16 @@ public class EAPOL extends BasePacket {
120 payloadData = this.payload.serialize(); 120 payloadData = this.payload.serialize();
121 } 121 }
122 122
123 - //prepare the buffer to hold the version (1), packet type (1), packet length (2) and the eap payload. 123 + // prepare the buffer to hold the version (1), packet type (1),
124 - //if there is no payload, packet length is 0 124 + // packet length (2) and the eap payload.
125 + // if there is no payload, packet length is 0
125 byte[] data = new byte[4 + this.packetLength]; 126 byte[] data = new byte[4 + this.packetLength];
126 final ByteBuffer bb = ByteBuffer.wrap(data); 127 final ByteBuffer bb = ByteBuffer.wrap(data);
127 bb.put(this.version); 128 bb.put(this.version);
128 bb.put(this.eapolType); 129 bb.put(this.eapolType);
129 bb.putShort(this.packetLength); 130 bb.putShort(this.packetLength);
130 131
131 - //put the EAP payload 132 + // put the EAP payload
132 if (payloadData != null) { 133 if (payloadData != null) {
133 bb.put(payloadData); 134 bb.put(payloadData);
134 } 135 }
...@@ -136,8 +137,6 @@ public class EAPOL extends BasePacket { ...@@ -136,8 +137,6 @@ public class EAPOL extends BasePacket {
136 return data; 137 return data;
137 } 138 }
138 139
139 -
140 -
141 @Override 140 @Override
142 public int hashCode() { 141 public int hashCode() {
143 final int prime = 3889; 142 final int prime = 3889;
...@@ -149,39 +148,13 @@ public class EAPOL extends BasePacket { ...@@ -149,39 +148,13 @@ public class EAPOL extends BasePacket {
149 } 148 }
150 149
151 /** 150 /**
151 + * Deserializer for EAPOL packets.
152 * 152 *
153 - * @param dstMac 153 + * @return deserializer
154 - * @param srcMac
155 - * @param eapolType
156 - * @param eap
157 - * @return Ethernet frame
158 */ 154 */
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 - }
181 -
182 public static Deserializer<EAPOL> deserializer() { 155 public static Deserializer<EAPOL> deserializer() {
183 return (data, offset, length) -> { 156 return (data, offset, length) -> {
184 - checkInput(data, offset, length, 0); 157 + checkInput(data, offset, length, HEADER_LENGTH);
185 158
186 EAPOL eapol = new EAPOL(); 159 EAPOL eapol = new EAPOL();
187 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); 160 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
...@@ -190,12 +163,14 @@ public class EAPOL extends BasePacket { ...@@ -190,12 +163,14 @@ public class EAPOL extends BasePacket {
190 eapol.setPacketLength(bb.getShort()); 163 eapol.setPacketLength(bb.getShort());
191 164
192 if (eapol.packetLength > 0) { 165 if (eapol.packetLength > 0) {
193 - //deserialize the EAP Payload 166 + checkHeaderLength(length, HEADER_LENGTH + eapol.packetLength);
194 - eapol.payload = new EAP(); 167 + // deserialize the EAP Payload
168 + eapol.payload = EAP.deserializer().deserialize(data,
169 + bb.position(), bb.limit() - bb.position());
195 170
196 - eapol.payload = eapol.payload.deserialize(data, bb.position(), length - 4);
197 eapol.payload.setParent(eapol); 171 eapol.payload.setParent(eapol);
198 } 172 }
173 +
199 return eapol; 174 return eapol;
200 }; 175 };
201 } 176 }
...@@ -205,24 +180,20 @@ public class EAPOL extends BasePacket { ...@@ -205,24 +180,20 @@ public class EAPOL extends BasePacket {
205 final int length) { 180 final int length) {
206 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); 181 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
207 182
208 - 183 + // deserialize the EAPOL header
209 - //deserialize the EAPOL header
210 this.version = bb.get(); 184 this.version = bb.get();
211 this.eapolType = bb.get(); 185 this.eapolType = bb.get();
212 this.packetLength = bb.getShort(); 186 this.packetLength = bb.getShort();
213 187
214 if (this.packetLength > 0) { 188 if (this.packetLength > 0) {
215 - //deserialize the EAP Payload 189 + // deserialize the EAP Payload
216 this.payload = new EAP(); 190 this.payload = new EAP();
217 191
218 this.payload = this.payload.deserialize(data, bb.position(), length - 4); 192 this.payload = this.payload.deserialize(data, bb.position(), length - 4);
219 this.payload.setParent(this); 193 this.payload.setParent(this);
220 } 194 }
221 195
222 -
223 return this; 196 return this;
224 } 197 }
225 -
226 -
227 } 198 }
228 199
......
...@@ -35,8 +35,9 @@ public class EthType { ...@@ -35,8 +35,9 @@ public class EthType {
35 VLAN(0x8100, "vlan", null), 35 VLAN(0x8100, "vlan", null),
36 BDDP(0x8942, "bddp", org.onlab.packet.LLDP.deserializer()), 36 BDDP(0x8942, "bddp", org.onlab.packet.LLDP.deserializer()),
37 MPLS_UNICAST(0x8847, "mpls_unicast", org.onlab.packet.MPLS.deserializer()), 37 MPLS_UNICAST(0x8847, "mpls_unicast", org.onlab.packet.MPLS.deserializer()),
38 - MPLS_MULTICAST(0x8848, "mpls_unicast", org.onlab.packet.MPLS.deserializer()); 38 + MPLS_MULTICAST(0x8848, "mpls_unicast", org.onlab.packet.MPLS.deserializer()),
39 - 39 + EAPOL(0x888e, "eapol", org.onlab.packet.EAPOL.deserializer()),
40 + UNKNOWN(0, "unknown", null);
40 41
41 42
42 private final EthType etherType; 43 private final EthType etherType;
...@@ -69,6 +70,15 @@ public class EthType { ...@@ -69,6 +70,15 @@ public class EthType {
69 return deserializer; 70 return deserializer;
70 } 71 }
71 72
73 + public static EtherType lookup(short etherType) {
74 + for (EtherType ethType : EtherType.values()) {
75 + if (ethType.ethType().toShort() == etherType) {
76 + return ethType;
77 + }
78 + }
79 + return UNKNOWN;
80 + }
81 +
72 } 82 }
73 83
74 84
......
...@@ -16,10 +16,8 @@ ...@@ -16,10 +16,8 @@
16 * 16 *
17 */ 17 */
18 18
19 -package org.onosproject.aaa.packet; 19 +package org.onlab.packet;
20 20
21 -import org.onlab.packet.BasePacket;
22 -import org.onlab.packet.IPacket;
23 import org.slf4j.Logger; 21 import org.slf4j.Logger;
24 22
25 import javax.crypto.Mac; 23 import javax.crypto.Mac;
...@@ -30,25 +28,28 @@ import java.nio.ByteBuffer; ...@@ -30,25 +28,28 @@ import java.nio.ByteBuffer;
30 import java.security.SecureRandom; 28 import java.security.SecureRandom;
31 import java.util.ArrayList; 29 import java.util.ArrayList;
32 import java.util.Arrays; 30 import java.util.Arrays;
31 +import java.util.List;
33 32
33 +import static org.onlab.packet.PacketUtils.checkHeaderLength;
34 +import static org.onlab.packet.PacketUtils.checkInput;
34 import static org.slf4j.LoggerFactory.getLogger; 35 import static org.slf4j.LoggerFactory.getLogger;
35 36
36 /** 37 /**
37 - * 38 + * RADIUS packet.
38 */ 39 */
39 public class RADIUS extends BasePacket { 40 public class RADIUS extends BasePacket {
40 protected byte code; 41 protected byte code;
41 protected byte identifier; 42 protected byte identifier;
42 protected short length = RADIUS_MIN_LENGTH; 43 protected short length = RADIUS_MIN_LENGTH;
43 protected byte[] authenticator = new byte[16]; 44 protected byte[] authenticator = new byte[16];
44 - protected ArrayList<RADIUSAttribute> attributes = new ArrayList<>(); 45 + protected List<RADIUSAttribute> attributes = new ArrayList<>();
45 46
46 - /* RADIUS parameters */ 47 + // RADIUS parameters
47 public static final short RADIUS_MIN_LENGTH = 20; 48 public static final short RADIUS_MIN_LENGTH = 20;
48 public static final short MAX_ATTR_VALUE_LENGTH = 253; 49 public static final short MAX_ATTR_VALUE_LENGTH = 253;
49 public static final short RADIUS_MAX_LENGTH = 4096; 50 public static final short RADIUS_MAX_LENGTH = 4096;
50 51
51 - /* RADIUS packet types */ 52 + // RADIUS packet types
52 public static final byte RADIUS_CODE_ACCESS_REQUEST = 0x01; 53 public static final byte RADIUS_CODE_ACCESS_REQUEST = 0x01;
53 public static final byte RADIUS_CODE_ACCESS_ACCEPT = 0x02; 54 public static final byte RADIUS_CODE_ACCESS_ACCEPT = 0x02;
54 public static final byte RADIUS_CODE_ACCESS_REJECT = 0x03; 55 public static final byte RADIUS_CODE_ACCESS_REJECT = 0x03;
...@@ -58,43 +59,92 @@ public class RADIUS extends BasePacket { ...@@ -58,43 +59,92 @@ public class RADIUS extends BasePacket {
58 59
59 private final Logger log = getLogger(getClass()); 60 private final Logger log = getLogger(getClass());
60 61
62 + /**
63 + * Default constructor.
64 + */
61 public RADIUS() { 65 public RADIUS() {
62 } 66 }
63 67
68 + /**
69 + * Constructs a RADIUS packet with the given code and identifier.
70 + *
71 + * @param code code
72 + * @param identifier identifier
73 + */
64 public RADIUS(byte code, byte identifier) { 74 public RADIUS(byte code, byte identifier) {
65 this.code = code; 75 this.code = code;
66 this.identifier = identifier; 76 this.identifier = identifier;
67 } 77 }
68 78
79 + /**
80 + * Gets the code.
81 + *
82 + * @return code
83 + */
69 public byte getCode() { 84 public byte getCode() {
70 return this.code; 85 return this.code;
71 } 86 }
72 87
88 + /**
89 + * Sets the code.
90 + *
91 + * @param code code
92 + */
73 public void setCode(byte code) { 93 public void setCode(byte code) {
74 this.code = code; 94 this.code = code;
75 } 95 }
76 96
97 + /**
98 + * Gets the identifier.
99 + *
100 + * @return identifier
101 + */
77 public byte getIdentifier() { 102 public byte getIdentifier() {
78 return this.identifier; 103 return this.identifier;
79 } 104 }
80 105
106 + /**
107 + * Sets the identifier.
108 + *
109 + * @param identifier identifier
110 + */
81 public void setIdentifier(byte identifier) { 111 public void setIdentifier(byte identifier) {
82 this.identifier = identifier; 112 this.identifier = identifier;
83 } 113 }
84 114
115 + /**
116 + * Gets the authenticator.
117 + *
118 + * @return authenticator
119 + */
85 public byte[] getAuthenticator() { 120 public byte[] getAuthenticator() {
86 return this.authenticator; 121 return this.authenticator;
87 } 122 }
88 123
89 - public void setAuthenticator(byte[] a) { 124 + /**
90 - this.authenticator = a; 125 + * Sets the authenticator.
126 + *
127 + * @param authenticator authenticator
128 + */
129 + public void setAuthenticator(byte[] authenticator) {
130 + this.authenticator = authenticator;
91 } 131 }
92 132
133 + /**
134 + * Generates an authenticator code.
135 + *
136 + * @return the authenticator
137 + */
93 public byte[] generateAuthCode() { 138 public byte[] generateAuthCode() {
94 new SecureRandom().nextBytes(this.authenticator); 139 new SecureRandom().nextBytes(this.authenticator);
95 return this.authenticator; 140 return this.authenticator;
96 } 141 }
97 142
143 + /**
144 + * Checks if the packet's code field is valid.
145 + *
146 + * @return whether the code is valid
147 + */
98 public boolean isValidCode() { 148 public boolean isValidCode() {
99 return this.code == RADIUS_CODE_ACCESS_REQUEST || 149 return this.code == RADIUS_CODE_ACCESS_REQUEST ||
100 this.code == RADIUS_CODE_ACCESS_ACCEPT || 150 this.code == RADIUS_CODE_ACCESS_ACCEPT ||
...@@ -104,11 +154,17 @@ public class RADIUS extends BasePacket { ...@@ -104,11 +154,17 @@ public class RADIUS extends BasePacket {
104 this.code == RADIUS_CODE_ACCESS_CHALLENGE; 154 this.code == RADIUS_CODE_ACCESS_CHALLENGE;
105 } 155 }
106 156
157 + /**
158 + * Adds a message authenticator to the packet based on the given key.
159 + *
160 + * @param key key to generate message authenticator
161 + * @return the messgae authenticator RADIUS attribute
162 + */
107 public RADIUSAttribute addMessageAuthenticator(String key) { 163 public RADIUSAttribute addMessageAuthenticator(String key) {
108 - /* Message-Authenticator = HMAC-MD5 (Type, Identifier, Length, Request Authenticator, Attributes) 164 + // Message-Authenticator = HMAC-MD5 (Type, Identifier, Length,
109 - When the message integrity check is calculated the signature string should be considered to be 165 + // Request Authenticator, Attributes)
110 - sixteen octets of zero. 166 + // When the message integrity check is calculated the signature string
111 - */ 167 + // should be considered to be sixteen octets of zero.
112 byte[] hashOutput = new byte[16]; 168 byte[] hashOutput = new byte[16];
113 Arrays.fill(hashOutput, (byte) 0); 169 Arrays.fill(hashOutput, (byte) 0);
114 170
...@@ -136,6 +192,13 @@ public class RADIUS extends BasePacket { ...@@ -136,6 +192,13 @@ public class RADIUS extends BasePacket {
136 return authAttribute; 192 return authAttribute;
137 } 193 }
138 194
195 + /**
196 + * Checks the message authenticator in the packet with one generated from
197 + * the given key.
198 + *
199 + * @param key key to generate message authenticator
200 + * @return whether the message authenticators match or not
201 + */
139 public boolean checkMessageAuthenticator(String key) { 202 public boolean checkMessageAuthenticator(String key) {
140 byte[] newHash = new byte[16]; 203 byte[] newHash = new byte[16];
141 Arrays.fill(newHash, (byte) 0); 204 Arrays.fill(newHash, (byte) 0);
...@@ -156,8 +219,10 @@ public class RADIUS extends BasePacket { ...@@ -156,8 +219,10 @@ public class RADIUS extends BasePacket {
156 } 219 }
157 220
158 /** 221 /**
159 - * @param message 222 + * Encapsulates an EAP packet in this RADIUS packet.
160 - * EAP message object to be embedded in the RADIUS EAP-Message attributed 223 + *
224 + * @param message EAP message object to be embedded in the RADIUS
225 + * EAP-Message attributed
161 */ 226 */
162 public void encapsulateMessage(EAP message) { 227 public void encapsulateMessage(EAP message) {
163 if (message.length <= MAX_ATTR_VALUE_LENGTH) { 228 if (message.length <= MAX_ATTR_VALUE_LENGTH) {
...@@ -193,6 +258,8 @@ public class RADIUS extends BasePacket { ...@@ -193,6 +258,8 @@ public class RADIUS extends BasePacket {
193 } 258 }
194 259
195 /** 260 /**
261 + * Decapsulates an EAP packet from the RADIUS packet.
262 + *
196 * @return An EAP object containing the reassembled EAP message 263 * @return An EAP object containing the reassembled EAP message
197 */ 264 */
198 public EAP decapsulateMessage() { 265 public EAP decapsulateMessage() {
...@@ -212,8 +279,9 @@ public class RADIUS extends BasePacket { ...@@ -212,8 +279,9 @@ public class RADIUS extends BasePacket {
212 } 279 }
213 280
214 /** 281 /**
215 - * @param attrType 282 + * Gets a list of attributes from the RADIUS packet.
216 - * the type field of the required attributes 283 + *
284 + * @param attrType the type field of the required attributes
217 * @return List of the attributes that matches the type or an empty list if there is none 285 * @return List of the attributes that matches the type or an empty list if there is none
218 */ 286 */
219 public ArrayList<RADIUSAttribute> getAttributeList(byte attrType) { 287 public ArrayList<RADIUSAttribute> getAttributeList(byte attrType) {
...@@ -227,8 +295,9 @@ public class RADIUS extends BasePacket { ...@@ -227,8 +295,9 @@ public class RADIUS extends BasePacket {
227 } 295 }
228 296
229 /** 297 /**
230 - * @param attrType 298 + * Gets an attribute from the RADIUS packet.
231 - * the type field of the required attribute 299 + *
300 + * @param attrType the type field of the required attribute
232 * @return the first attribute that matches the type or null if does not exist 301 * @return the first attribute that matches the type or null if does not exist
233 */ 302 */
234 public RADIUSAttribute getAttribute(byte attrType) { 303 public RADIUSAttribute getAttribute(byte attrType) {
...@@ -241,10 +310,10 @@ public class RADIUS extends BasePacket { ...@@ -241,10 +310,10 @@ public class RADIUS extends BasePacket {
241 } 310 }
242 311
243 /** 312 /**
244 - * @param attrType 313 + * Sets an attribute in the RADIUS packet.
245 - * the type field of the attribute to set 314 + *
246 - * @param value 315 + * @param attrType the type field of the attribute to set
247 - * value to be set 316 + * @param value value to be set
248 * @return reference to the attribute object 317 * @return reference to the attribute object
249 */ 318 */
250 public RADIUSAttribute setAttribute(byte attrType, byte[] value) { 319 public RADIUSAttribute setAttribute(byte attrType, byte[] value) {
...@@ -255,6 +324,13 @@ public class RADIUS extends BasePacket { ...@@ -255,6 +324,13 @@ public class RADIUS extends BasePacket {
255 return newAttribute; 324 return newAttribute;
256 } 325 }
257 326
327 + /**
328 + * Updates an attribute in the RADIUS packet.
329 + *
330 + * @param attrType the type field of the attribute to update
331 + * @param value the value to update to
332 + * @return reference to the attribute object
333 + */
258 public RADIUSAttribute updateAttribute(byte attrType, byte[] value) { 334 public RADIUSAttribute updateAttribute(byte attrType, byte[] value) {
259 for (int i = 0; i < this.attributes.size(); i++) { 335 for (int i = 0; i < this.attributes.size(); i++) {
260 if (this.attributes.get(i).getType() == attrType) { 336 if (this.attributes.get(i).getType() == attrType) {
...@@ -268,6 +344,40 @@ public class RADIUS extends BasePacket { ...@@ -268,6 +344,40 @@ public class RADIUS extends BasePacket {
268 return null; 344 return null;
269 } 345 }
270 346
347 + /**
348 + * Deserializer for RADIUS packets.
349 + *
350 + * @return deserializer
351 + */
352 + public static Deserializer<RADIUS> deserializer() {
353 + return (data, offset, length) -> {
354 + checkInput(data, offset, length, RADIUS_MIN_LENGTH);
355 +
356 + final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
357 + RADIUS radius = new RADIUS();
358 + radius.code = bb.get();
359 + radius.identifier = bb.get();
360 + radius.length = bb.getShort();
361 + bb.get(radius.authenticator, 0, 16);
362 +
363 + checkHeaderLength(length, radius.length);
364 +
365 + int remainingLength = radius.length - RADIUS_MIN_LENGTH;
366 + while (remainingLength > 0 && bb.hasRemaining()) {
367 +
368 + RADIUSAttribute attr = new RADIUSAttribute();
369 + attr.setType(bb.get());
370 + attr.setLength(bb.get());
371 + short attrLength = (short) (attr.length & 0xff);
372 + attr.value = new byte[attrLength - 2];
373 + bb.get(attr.value, 0, attrLength - 2);
374 + radius.attributes.add(attr);
375 + remainingLength -= attr.length;
376 + }
377 + return radius;
378 + };
379 + }
380 +
271 @Override 381 @Override
272 public byte[] serialize() { 382 public byte[] serialize() {
273 final byte[] data = new byte[this.length]; 383 final byte[] data = new byte[this.length];
......
...@@ -16,16 +16,17 @@ ...@@ -16,16 +16,17 @@
16 * 16 *
17 */ 17 */
18 18
19 -package org.onosproject.aaa.packet; 19 +package org.onlab.packet;
20 -
21 -import java.nio.ByteBuffer;
22 20
21 +/**
22 + * An attribute in a RADIUS packet.
23 + */
23 public class RADIUSAttribute { 24 public class RADIUSAttribute {
24 protected byte type; 25 protected byte type;
25 protected byte length; 26 protected byte length;
26 protected byte[] value; 27 protected byte[] value;
27 28
28 - /* RADIUS attribute types */ 29 + // RADIUS attribute types
29 public static final byte RADIUS_ATTR_USERNAME = 1; 30 public static final byte RADIUS_ATTR_USERNAME = 1;
30 public static final byte RADIUS_ATTR_NAS_IP = 4; 31 public static final byte RADIUS_ATTR_NAS_IP = 4;
31 public static final byte RADIUS_ATTR_NAS_PORT = 5; 32 public static final byte RADIUS_ATTR_NAS_PORT = 5;
...@@ -40,15 +41,30 @@ public class RADIUSAttribute { ...@@ -40,15 +41,30 @@ public class RADIUSAttribute {
40 public static final byte RADIUS_ATTR_MESSAGE_AUTH = 80; 41 public static final byte RADIUS_ATTR_MESSAGE_AUTH = 80;
41 public static final byte RADIUS_ATTR_NAS_PORT_ID = 87; 42 public static final byte RADIUS_ATTR_NAS_PORT_ID = 87;
42 43
44 + /**
45 + * Default constructor.
46 + */
43 public RADIUSAttribute() { 47 public RADIUSAttribute() {
44 } 48 }
45 49
50 + /**
51 + * Constructs a RADIUS attribute with the give type, length and value.
52 + *
53 + * @param type type
54 + * @param length length
55 + * @param value value
56 + */
46 public RADIUSAttribute(final byte type, final byte length, final byte[] value) { 57 public RADIUSAttribute(final byte type, final byte length, final byte[] value) {
47 this.type = type; 58 this.type = type;
48 this.length = length; 59 this.length = length;
49 this.value = value; 60 this.value = value;
50 } 61 }
51 62
63 + /**
64 + * Checks if the attribute type is valid.
65 + *
66 + * @return whether the type is valid or not
67 + */
52 public boolean isValidType() { 68 public boolean isValidType() {
53 return this.type == RADIUS_ATTR_USERNAME || 69 return this.type == RADIUS_ATTR_USERNAME ||
54 this.type == RADIUS_ATTR_NAS_IP || 70 this.type == RADIUS_ATTR_NAS_IP ||
...@@ -64,6 +80,8 @@ public class RADIUSAttribute { ...@@ -64,6 +80,8 @@ public class RADIUSAttribute {
64 } 80 }
65 81
66 /** 82 /**
83 + * Gets the attribute type.
84 + *
67 * @return the type 85 * @return the type
68 */ 86 */
69 public byte getType() { 87 public byte getType() {
...@@ -71,8 +89,9 @@ public class RADIUSAttribute { ...@@ -71,8 +89,9 @@ public class RADIUSAttribute {
71 } 89 }
72 90
73 /** 91 /**
74 - * @param type 92 + * Sets the attribute type.
75 - * the code to set 93 + *
94 + * @param type the code to set
76 * @return this 95 * @return this
77 */ 96 */
78 public RADIUSAttribute setType(final byte type) { 97 public RADIUSAttribute setType(final byte type) {
...@@ -81,6 +100,8 @@ public class RADIUSAttribute { ...@@ -81,6 +100,8 @@ public class RADIUSAttribute {
81 } 100 }
82 101
83 /** 102 /**
103 + * Gets the attribute length.
104 + *
84 * @return the length 105 * @return the length
85 */ 106 */
86 public byte getLength() { 107 public byte getLength() {
...@@ -88,8 +109,9 @@ public class RADIUSAttribute { ...@@ -88,8 +109,9 @@ public class RADIUSAttribute {
88 } 109 }
89 110
90 /** 111 /**
91 - * @param length 112 + * Sets the attribute length.
92 - * the length to set 113 + *
114 + * @param length the length to set
93 * @return this 115 * @return this
94 */ 116 */
95 public RADIUSAttribute setLength(final byte length) { 117 public RADIUSAttribute setLength(final byte length) {
...@@ -98,6 +120,8 @@ public class RADIUSAttribute { ...@@ -98,6 +120,8 @@ public class RADIUSAttribute {
98 } 120 }
99 121
100 /** 122 /**
123 + * Gets the attribute value.
124 + *
101 * @return the value 125 * @return the value
102 */ 126 */
103 public byte[] getValue() { 127 public byte[] getValue() {
...@@ -105,8 +129,9 @@ public class RADIUSAttribute { ...@@ -105,8 +129,9 @@ public class RADIUSAttribute {
105 } 129 }
106 130
107 /** 131 /**
108 - * @param value 132 + * Sets the attribute value.
109 - * the data to set 133 + *
134 + * @param value the data to set
110 * @return this 135 * @return this
111 */ 136 */
112 public RADIUSAttribute setValue(final byte[] value) { 137 public RADIUSAttribute setValue(final byte[] value) {
...@@ -114,12 +139,4 @@ public class RADIUSAttribute { ...@@ -114,12 +139,4 @@ public class RADIUSAttribute {
114 return this; 139 return this;
115 } 140 }
116 141
117 - public byte[] serialize() {
118 - final byte[] data = new byte[this.length];
119 - final ByteBuffer bb = ByteBuffer.wrap(data);
120 - bb.put(this.type);
121 - bb.put(this.length);
122 - bb.put(this.value);
123 - return data;
124 - }
125 } 142 }
......