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
diff --git a/apps/aaa/src/main/java/org/onosproject/aaa/AAA.java b/apps/aaa/src/main/java/org/onosproject/aaa/AAA.java
index 9168017..5023b54 100644
--- a/apps/aaa/src/main/java/org/onosproject/aaa/AAA.java
+++ b/apps/aaa/src/main/java/org/onosproject/aaa/AAA.java
@@ -16,6 +16,7 @@
package org.onosproject.aaa;
import com.google.common.base.Strings;
+import com.google.common.collect.Maps;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
@@ -23,19 +24,20 @@
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onlab.packet.DeserializationException;
+import org.onlab.packet.EAP;
+import org.onlab.packet.EAPOL;
+import org.onlab.packet.EthType;
import org.onlab.packet.Ethernet;
import org.onlab.packet.IPv4;
import org.onlab.packet.Ip4Address;
import org.onlab.packet.IpAddress;
import org.onlab.packet.MacAddress;
+import org.onlab.packet.RADIUS;
+import org.onlab.packet.RADIUSAttribute;
import org.onlab.packet.UDP;
import org.onlab.packet.VlanId;
import org.onlab.util.Tools;
-import org.onosproject.aaa.packet.EAP;
-import org.onosproject.aaa.packet.EAPEthernet;
-import org.onosproject.aaa.packet.EAPOL;
-import org.onosproject.aaa.packet.RADIUS;
-import org.onosproject.aaa.packet.RADIUSAttribute;
import org.onosproject.cfg.ComponentConfigService;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
@@ -45,11 +47,9 @@
import org.onosproject.net.PortNumber;
import org.onosproject.net.flow.DefaultTrafficSelector;
import org.onosproject.net.flow.DefaultTrafficTreatment;
-import org.onosproject.net.flow.FlowRuleService;
import org.onosproject.net.flow.TrafficSelector;
import org.onosproject.net.flow.TrafficTreatment;
import org.onosproject.net.host.HostService;
-import org.onosproject.net.intent.IntentService;
import org.onosproject.net.packet.DefaultOutboundPacket;
import org.onosproject.net.packet.InboundPacket;
import org.onosproject.net.packet.OutboundPacket;
@@ -57,7 +57,6 @@
import org.onosproject.net.packet.PacketPriority;
import org.onosproject.net.packet.PacketProcessor;
import org.onosproject.net.packet.PacketService;
-import org.onosproject.net.topology.TopologyService;
import org.onosproject.xosintegration.VoltTenantService;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
@@ -67,7 +66,6 @@
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.Dictionary;
-import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
@@ -77,7 +75,7 @@
/**
- * AAA application for Onos.
+ * AAA application for ONOS.
*/
@Component(immediate = true)
public class AAA {
@@ -86,21 +84,10 @@
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected CoreService coreService;
- // topology information
- @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
- protected TopologyService topologyService;
-
// to receive Packet-in events that we'll respond to
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected PacketService packetService;
- @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
- protected FlowRuleService flowService;
-
- // to submit/withdraw intents for traffic manipulation
- @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
- protected IntentService intentService;
-
// end host information
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected HostService hostService;
@@ -108,7 +95,6 @@
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected VoltTenantService voltTenantService;
-
// for verbose output
private final Logger log = getLogger(getClass());
@@ -130,13 +116,13 @@
private static final int DEFAULT_RADIUS_UPLINK = 2;
// RADIUS server shared secret
private static final String DEFAULT_RADIUS_SECRET = "ONOSecret";
- //RADIUS MAC address
+ // RADIUS MAC address
private static final String RADIUS_MAC_ADDRESS = "00:00:00:00:01:10";
- //NAS MAC address
+ // NAS MAC address
private static final String NAS_MAC_ADDRESS = "00:00:00:00:10:01";
- //Radius Switch Id
+ // Radius Switch Id
private static final String DEFAULT_RADIUS_SWITCH = "of:5e3e486e73000187";
- //Radius Port Number
+ // Radius Port Number
private static final String DEFAULT_RADIUS_PORT = "5";
@Property(name = "radiusIpAddress", value = DEFAULT_RADIUS_IP,
@@ -221,13 +207,12 @@
packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 2);
TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
- selector.matchEthType(EAPEthernet.TYPE_PAE);
+ selector.matchEthType(EthType.EtherType.EAPOL.ethType().toShort());
packetService.requestPackets(selector.build(),
PacketPriority.CONTROL, appId);
// Instantiate the map of the state machines
- Map<String, StateMachine> stateMachines = new HashMap<String, StateMachine>();
- stateMachineMap = Collections.synchronizedMap(stateMachines);
+ stateMachineMap = Collections.synchronizedMap(Maps.newHashMap());
hostService.startMonitoringIp(IpAddress.valueOf(radiusIpAddress));
@@ -241,6 +226,38 @@
processor = null;
}
+ /**
+ * Builds an EAPOL packet based on the given parameters.
+ *
+ * @param dstMac destination MAC address
+ * @param srcMac source MAC address
+ * @param eapolType EAPOL type
+ * @param eap EAP payload
+ * @return Ethernet frame
+ */
+ private static Ethernet buildEapolResponse(MacAddress dstMac, MacAddress srcMac,
+ short vlan, byte eapolType, EAP eap) {
+
+ Ethernet eth = new Ethernet();
+ eth.setDestinationMACAddress(dstMac.toBytes());
+ eth.setSourceMACAddress(srcMac.toBytes());
+ eth.setEtherType(EthType.EtherType.EAPOL.ethType().toShort());
+ if (vlan != Ethernet.VLAN_UNTAGGED) {
+ eth.setVlanID(vlan);
+ }
+ //eapol header
+ EAPOL eapol = new EAPOL();
+ eapol.setEapolType(eapolType);
+ eapol.setPacketLength(eap.getLength());
+
+ //eap part
+ eapol.setPayload(eap);
+
+ eth.setPayload(eapol);
+ eth.setPad(true);
+ return eth;
+ }
+
// our handler defined as a private inner class
/**
@@ -256,22 +273,27 @@
if (ethPkt == null) {
return;
}
- //identify if incoming packet comes from supplicant (EAP) or RADIUS
- switch (ethPkt.getEtherType()) {
- case (short) 0x888e:
- handleSupplicantPacket(ethPkt, context);
+ // identify if incoming packet comes from supplicant (EAP) or RADIUS
+ switch (EthType.EtherType.lookup(ethPkt.getEtherType())) {
+ case EAPOL:
+ handleSupplicantPacket(context.inPacket());
break;
- case 0x800:
+ case IPV4:
IPv4 ipv4Packet = (IPv4) ethPkt.getPayload();
Ip4Address srcIp = Ip4Address.valueOf(ipv4Packet.getSourceAddress());
Ip4Address radiusIp4Address = Ip4Address.valueOf(parsedRadiusIpAddress);
if (srcIp.equals(radiusIp4Address) && ipv4Packet.getProtocol() == IPv4.PROTOCOL_UDP) {
// TODO: check for port as well when it's configurable
UDP udpPacket = (UDP) ipv4Packet.getPayload();
- // RADIUS radiusPacket = (RADIUS) udpPacket.getPayload();
+
byte[] datagram = udpPacket.getPayload().serialize();
- RADIUS radiusPacket = new RADIUS();
- radiusPacket = (RADIUS) radiusPacket.deserialize(datagram, 0, datagram.length);
+ RADIUS radiusPacket;
+ try {
+ radiusPacket = RADIUS.deserializer().deserialize(datagram, 0, datagram.length);
+ } catch (DeserializationException e) {
+ log.warn("Unable to deserialize RADIUS packet:", e);
+ return;
+ }
handleRadiusPacket(radiusPacket);
}
break;
@@ -282,33 +304,33 @@
/**
- * Handle PAE packets (supplicant).
- * @param ethPkt Ethernet packet coming from the supplicant.
+ * Handles PAE packets (supplicant).
+ *
+ * @param inPacket Ethernet packet coming from the supplicant
*/
- private void handleSupplicantPacket(Ethernet ethPkt, PacketContext context) {
+ private void handleSupplicantPacket(InboundPacket inPacket) {
+ Ethernet ethPkt = inPacket.parsed();
// Where does it come from?
MacAddress srcMAC = ethPkt.getSourceMAC();
- DeviceId deviceId = context.inPacket().receivedFrom().deviceId();
- PortNumber portNumber = context.inPacket().receivedFrom().port();
+ DeviceId deviceId = inPacket.receivedFrom().deviceId();
+ PortNumber portNumber = inPacket.receivedFrom().port();
String sessionId = deviceId.toString() + portNumber.toString();
StateMachine stateMachine = getStateMachine(sessionId);
- //Reserialize the data of the eth packet into our EAPOL format
- // this code will go once it is in the onos repository.
- byte[] bullshit = ethPkt.getPayload().serialize();
- EAPOL eapol = (EAPOL) new EAPOL().deserialize(bullshit, 0, bullshit.length);
+
+ EAPOL eapol = (EAPOL) ethPkt.getPayload();
switch (eapol.getEapolType()) {
case EAPOL.EAPOL_START:
try {
stateMachine.start();
- stateMachine.supplicantConnectpoint = context.inPacket().receivedFrom();
+ stateMachine.supplicantConnectpoint = inPacket.receivedFrom();
//send an EAP Request/Identify to the supplicant
EAP eapPayload = new EAP(EAP.REQUEST, stateMachine.getIdentifier(), EAP.ATTR_IDENTITY, null);
- Ethernet eth = EAPOL.buildEapolResponse(srcMAC, MacAddress.valueOf(1L),
- ethPkt.getVlanID(), EAPOL.EAPOL_PACKET,
- eapPayload);
+ Ethernet eth = buildEapolResponse(srcMAC, MacAddress.valueOf(1L),
+ ethPkt.getVlanID(), EAPOL.EAPOL_PACKET,
+ eapPayload);
stateMachine.supplicantAddress = srcMAC;
stateMachine.vlanId = ethPkt.getVlanID();
@@ -319,7 +341,7 @@
break;
case EAPOL.EAPOL_PACKET:
- //check if this is a Response/Idenfity or a Response/TLS
+ //check if this is a Response/Identify or a Response/TLS
EAP eapPacket = (EAP) eapol.getPayload();
byte dataType = eapPacket.getDataType();
@@ -406,7 +428,8 @@
}
/**
- * Handle RADIUS packets.
+ * Handles RADIUS packets.
+ *
* @param radiusPacket RADIUS packet coming from the RADIUS server.
*/
private void handleRadiusPacket(RADIUS radiusPacket) {
@@ -416,7 +439,6 @@
return;
}
- byte[] eapMessage = null;
EAP eapPayload = new EAP();
Ethernet eth = null;
switch (radiusPacket.getCode()) {
@@ -424,18 +446,18 @@
byte[] challengeState = radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_STATE).getValue();
eapPayload = radiusPacket.decapsulateMessage();
stateMachine.setChallengeInfo(eapPayload.getIdentifier(), challengeState);
- eth = EAPOL.buildEapolResponse(stateMachine.supplicantAddress,
+ eth = buildEapolResponse(stateMachine.supplicantAddress,
MacAddress.valueOf(1L), stateMachine.vlanId, EAPOL.EAPOL_PACKET, eapPayload);
this.sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint);
break;
case RADIUS.RADIUS_CODE_ACCESS_ACCEPT:
try {
//send an EAPOL - Success to the supplicant.
- eapMessage =
+ byte[] eapMessage =
radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE).getValue();
eapPayload = new EAP();
eapPayload = (EAP) eapPayload.deserialize(eapMessage, 0, eapMessage.length);
- eth = EAPOL.buildEapolResponse(stateMachine.supplicantAddress,
+ eth = buildEapolResponse(stateMachine.supplicantAddress,
MacAddress.valueOf(1L), stateMachine.vlanId, EAPOL.EAPOL_PACKET, eapPayload);
this.sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint);
@@ -539,10 +561,11 @@
}
-
/**
* Send the ethernet packet to the supplicant.
+ *
* @param ethernetPkt the ethernet packet
+ * @param connectPoint the connect point to send out
*/
private void sendPacketToSupplicant(Ethernet ethernetPkt, ConnectPoint connectPoint) {
TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(connectPoint.port()).build();
diff --git a/apps/aaa/src/main/java/org/onosproject/aaa/packet/EAPEthernet.java b/apps/aaa/src/main/java/org/onosproject/aaa/packet/EAPEthernet.java
deleted file mode 100644
index f975c15..0000000
--- a/apps/aaa/src/main/java/org/onosproject/aaa/packet/EAPEthernet.java
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * Copyright 2015 Open Networking Laboratory
- *
- * 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.onosproject.aaa.packet;
-
-import org.onlab.packet.Deserializer;
-import org.onlab.packet.EthType;
-import org.onlab.packet.Ethernet;
-import org.onlab.packet.IPacket;
-
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * Created by jono on 5/19/15.
- */
-public final class EAPEthernet extends Ethernet {
-
- public static final short TYPE_PAE = (short) 0x888e;
-
- private static final Map<Short, Deserializer<? extends IPacket>> ETHERTYPE_DESERIALIZER_MAP =
- new HashMap<>();
-
- private EAPEthernet() {
-
- }
-
- static {
- for (EthType.EtherType ethType : EthType.EtherType.values()) {
- if (ethType.deserializer() != null) {
- ETHERTYPE_DESERIALIZER_MAP.put(ethType.ethType().toShort(),
- ethType.deserializer());
- }
- }
- ETHERTYPE_DESERIALIZER_MAP.put((short) 0x888e, EAPOL.deserializer());
- }
-
-}
diff --git a/apps/aaa/src/main/java/org/onosproject/aaa/packet/EAP.java b/utils/misc/src/main/java/org/onlab/packet/EAP.java
similarity index 75%
rename from apps/aaa/src/main/java/org/onosproject/aaa/packet/EAP.java
rename to utils/misc/src/main/java/org/onlab/packet/EAP.java
index ed2718e..3fba4c9 100644
--- a/apps/aaa/src/main/java/org/onosproject/aaa/packet/EAP.java
+++ b/utils/misc/src/main/java/org/onlab/packet/EAP.java
@@ -16,29 +16,30 @@
*
*/
-package org.onosproject.aaa.packet;
-
-import org.onlab.packet.BasePacket;
-import org.onlab.packet.IPacket;
+package org.onlab.packet;
import java.nio.ByteBuffer;
+import static org.onlab.packet.PacketUtils.checkHeaderLength;
+import static org.onlab.packet.PacketUtils.checkInput;
/**
- *
+ * EAP (Extensible Authentication Protocol) packet.
*/
public class EAP extends BasePacket {
+ private static final int HEADER_LENGTH = 4;
+
public static final short MIN_LEN = 0x4;
public static final short EAP_HDR_LEN_REQ_RESP = 5;
public static final short EAP_HDR_LEN_SUC_FAIL = 4;
- /* EAP Code */
+ // EAP Code
public static final byte REQUEST = 0x1;
public static final byte RESPONSE = 0x2;
public static final byte SUCCESS = 0x3;
public static final byte FAILURE = 0x4;
- /* EAP Attribute Type */
+ // EAP Attribute Type
public static final byte ATTR_IDENTITY = 0x1;
public static final byte ATTR_NOTIFICATION = 0x2;
public static final byte ATTR_NAK = 0x3;
@@ -55,7 +56,8 @@
/**
- * Get the EAP code.
+ * Gets the EAP code.
+ *
* @return EAP code
*/
public byte getCode() {
@@ -64,7 +66,8 @@
/**
- * Set the EAP code.
+ * Sets the EAP code.
+ *
* @param code EAP code
* @return this
*/
@@ -74,7 +77,8 @@
}
/**
- * Get the EAP identifier.
+ * Gets the EAP identifier.
+ *
* @return EAP identifier
*/
public byte getIdentifier() {
@@ -82,7 +86,8 @@
}
/**
- * Set the EAP identifier.
+ * Sets the EAP identifier.
+ *
* @param identifier
* @return this
*/
@@ -92,7 +97,8 @@
}
/**
- * Get the get packet length.
+ * Gets the get packet length.
+ *
* @return packet length
*/
public short getLength() {
@@ -100,7 +106,8 @@
}
/**
- * Set the packet length.
+ * Sets the packet length.
+ *
* @param length packet length
* @return this
*/
@@ -110,7 +117,8 @@
}
/**
- * Get the data type.
+ * Gets the data type.
+ *
* @return data type
*/
public byte getDataType() {
@@ -118,7 +126,8 @@
}
/**
- * Set the data type.
+ * Sets the data type.
+ *
* @param type data type
* @return this
*/
@@ -128,7 +137,8 @@
}
/**
- * Get the EAP data.
+ * Gets the EAP data.
+ *
* @return EAP data
*/
public byte[] getData() {
@@ -136,7 +146,8 @@
}
/**
- * Set the EAP data.
+ * Sets the EAP data.
+ *
* @param data EAP data to be set
* @return this
*/
@@ -146,7 +157,7 @@
}
/**
- * Default EAP constructor that set the EAP code to 0.
+ * Default EAP constructor that sets the EAP code to 0.
*/
public EAP() {
this.code = 0;
@@ -154,6 +165,7 @@
/**
* EAP constructor that initially sets all fields.
+ *
* @param code EAP code
* @param identifier EAP identifier
* @param type packet type
@@ -172,10 +184,36 @@
}
/**
- * Serializes the packet, based on the code/type using the payload
- * to compute its length.
- * @return the serialized payload
+ * Deserializer for EAP packets.
+ *
+ * @return deserializer
*/
+ public static Deserializer<EAP> deserializer() {
+ return (data, offset, length) -> {
+ checkInput(data, offset, length, HEADER_LENGTH);
+
+ EAP eap = new EAP();
+ final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
+ eap.code = bb.get();
+ eap.identifier = bb.get();
+ eap.length = bb.getShort();
+
+ checkHeaderLength(length, HEADER_LENGTH + eap.length);
+
+ int dataLength;
+ if (eap.code == REQUEST || eap.code == RESPONSE) {
+ eap.type = bb.get();
+ dataLength = eap.length - 5;
+ } else {
+ dataLength = eap.length - 4;
+ }
+
+ eap.data = new byte[dataLength];
+ bb.get(eap.data);
+ return eap;
+ };
+ }
+
@Override
public byte[] serialize() {
final byte[] data = new byte[this.length];
diff --git a/apps/aaa/src/main/java/org/onosproject/aaa/packet/EAPOL.java b/utils/misc/src/main/java/org/onlab/packet/EAPOL.java
similarity index 70%
rename from apps/aaa/src/main/java/org/onosproject/aaa/packet/EAPOL.java
rename to utils/misc/src/main/java/org/onlab/packet/EAPOL.java
index ac11dcc..1820cc3 100644
--- a/apps/aaa/src/main/java/org/onosproject/aaa/packet/EAPOL.java
+++ b/utils/misc/src/main/java/org/onlab/packet/EAPOL.java
@@ -16,20 +16,15 @@
*
*/
-package org.onosproject.aaa.packet;
-
-import org.onlab.packet.BasePacket;
-import org.onlab.packet.Deserializer;
-import org.onlab.packet.Ethernet;
-import org.onlab.packet.IPacket;
-import org.onlab.packet.MacAddress;
+package org.onlab.packet;
import java.nio.ByteBuffer;
+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 {
@@ -37,7 +32,9 @@
private byte eapolType;
private short packetLength;
- /* EAPOL Packet Type */
+ 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;
@@ -48,9 +45,9 @@
(byte) 0x01, (byte) 0x80, (byte) 0xc2, (byte) 0x00, (byte) 0x00, (byte) 0x03
});
-
/**
- * Get version.
+ * Gets the version.
+ *
* @return version
*/
public byte getVersion() {
@@ -58,7 +55,8 @@
}
/**
- * Set version.
+ * Sets the version.
+ *
* @param version EAPOL version
* @return this
*/
@@ -68,7 +66,8 @@
}
/**
- * Get type.
+ * Gets the type.
+ *
* @return EAPOL type
*/
public byte getEapolType() {
@@ -76,7 +75,8 @@
}
/**
- * Set EAPOL type.
+ * Sets the EAPOL type.
+ *
* @param eapolType EAPOL type
* @return this
*/
@@ -86,7 +86,8 @@
}
/**
- * Get packet length.
+ * Gets the packet length.
+ *
* @return packet length
*/
public short getPacketLength() {
@@ -94,7 +95,8 @@
}
/**
- * Set packet length.
+ * Sets the packet length.
+ *
* @param packetLen packet length
* @return this
*/
@@ -103,16 +105,14 @@
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) {
@@ -120,15 +120,16 @@
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
+ // 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
+ // put the EAP payload
if (payloadData != null) {
bb.put(payloadData);
}
@@ -136,8 +137,6 @@
return data;
}
-
-
@Override
public int hashCode() {
final int prime = 3889;
@@ -149,39 +148,13 @@
}
/**
+ * Deserializer for EAPOL packets.
*
- * @param dstMac
- * @param srcMac
- * @param eapolType
- * @param eap
- * @return Ethernet frame
+ * @return deserializer
*/
- public static Ethernet buildEapolResponse(MacAddress dstMac, MacAddress srcMac,
- short vlan, byte eapolType, EAP eap) {
-
- Ethernet eth = new Ethernet();
- eth.setDestinationMACAddress(dstMac.toBytes());
- eth.setSourceMACAddress(srcMac.toBytes());
- eth.setEtherType(EAPEthernet.TYPE_PAE);
- if (vlan != Ethernet.VLAN_UNTAGGED) {
- eth.setVlanID(vlan);
- }
- //eapol header
- EAPOL eapol = new EAPOL();
- eapol.setEapolType(eapolType);
- eapol.setPacketLength(eap.getLength());
-
- //eap part
- eapol.setPayload(eap);
-
- eth.setPayload(eapol);
- eth.setPad(true);
- return eth;
- }
-
public static Deserializer<EAPOL> deserializer() {
return (data, offset, length) -> {
- checkInput(data, offset, length, 0);
+ checkInput(data, offset, length, HEADER_LENGTH);
EAPOL eapol = new EAPOL();
final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
@@ -190,12 +163,14 @@
eapol.setPacketLength(bb.getShort());
if (eapol.packetLength > 0) {
- //deserialize the EAP Payload
- eapol.payload = new EAP();
+ checkHeaderLength(length, HEADER_LENGTH + eapol.packetLength);
+ // deserialize the EAP Payload
+ eapol.payload = EAP.deserializer().deserialize(data,
+ bb.position(), bb.limit() - bb.position());
- eapol.payload = eapol.payload.deserialize(data, bb.position(), length - 4);
eapol.payload.setParent(eapol);
}
+
return eapol;
};
}
@@ -205,24 +180,20 @@
final int length) {
final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
-
- //deserialize the EAPOL header
+ // deserialize the EAPOL header
this.version = bb.get();
this.eapolType = bb.get();
this.packetLength = bb.getShort();
if (this.packetLength > 0) {
- //deserialize the EAP Payload
+ // deserialize the EAP Payload
this.payload = new EAP();
this.payload = this.payload.deserialize(data, bb.position(), length - 4);
this.payload.setParent(this);
}
-
return this;
}
-
-
}
diff --git a/utils/misc/src/main/java/org/onlab/packet/EthType.java b/utils/misc/src/main/java/org/onlab/packet/EthType.java
index 90439eb..561c930 100644
--- a/utils/misc/src/main/java/org/onlab/packet/EthType.java
+++ b/utils/misc/src/main/java/org/onlab/packet/EthType.java
@@ -35,8 +35,9 @@
VLAN(0x8100, "vlan", null),
BDDP(0x8942, "bddp", org.onlab.packet.LLDP.deserializer()),
MPLS_UNICAST(0x8847, "mpls_unicast", org.onlab.packet.MPLS.deserializer()),
- MPLS_MULTICAST(0x8848, "mpls_unicast", org.onlab.packet.MPLS.deserializer());
-
+ MPLS_MULTICAST(0x8848, "mpls_unicast", org.onlab.packet.MPLS.deserializer()),
+ EAPOL(0x888e, "eapol", org.onlab.packet.EAPOL.deserializer()),
+ UNKNOWN(0, "unknown", null);
private final EthType etherType;
@@ -69,6 +70,15 @@
return deserializer;
}
+ public static EtherType lookup(short etherType) {
+ for (EtherType ethType : EtherType.values()) {
+ if (ethType.ethType().toShort() == etherType) {
+ return ethType;
+ }
+ }
+ return UNKNOWN;
+ }
+
}
diff --git a/apps/aaa/src/main/java/org/onosproject/aaa/packet/RADIUS.java b/utils/misc/src/main/java/org/onlab/packet/RADIUS.java
similarity index 72%
rename from apps/aaa/src/main/java/org/onosproject/aaa/packet/RADIUS.java
rename to utils/misc/src/main/java/org/onlab/packet/RADIUS.java
index 753490b..297fee7 100644
--- a/apps/aaa/src/main/java/org/onosproject/aaa/packet/RADIUS.java
+++ b/utils/misc/src/main/java/org/onlab/packet/RADIUS.java
@@ -16,10 +16,8 @@
*
*/
-package org.onosproject.aaa.packet;
+package org.onlab.packet;
-import org.onlab.packet.BasePacket;
-import org.onlab.packet.IPacket;
import org.slf4j.Logger;
import javax.crypto.Mac;
@@ -30,25 +28,28 @@
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.List;
+import static org.onlab.packet.PacketUtils.checkHeaderLength;
+import static org.onlab.packet.PacketUtils.checkInput;
import static org.slf4j.LoggerFactory.getLogger;
/**
- *
+ * RADIUS packet.
*/
public class RADIUS extends BasePacket {
protected byte code;
protected byte identifier;
protected short length = RADIUS_MIN_LENGTH;
protected byte[] authenticator = new byte[16];
- protected ArrayList<RADIUSAttribute> attributes = new ArrayList<>();
+ protected List<RADIUSAttribute> attributes = new ArrayList<>();
- /* RADIUS parameters */
+ // RADIUS parameters
public static final short RADIUS_MIN_LENGTH = 20;
public static final short MAX_ATTR_VALUE_LENGTH = 253;
public static final short RADIUS_MAX_LENGTH = 4096;
- /* RADIUS packet types */
+ // RADIUS packet types
public static final byte RADIUS_CODE_ACCESS_REQUEST = 0x01;
public static final byte RADIUS_CODE_ACCESS_ACCEPT = 0x02;
public static final byte RADIUS_CODE_ACCESS_REJECT = 0x03;
@@ -58,43 +59,92 @@
private final Logger log = getLogger(getClass());
+ /**
+ * Default constructor.
+ */
public RADIUS() {
}
+ /**
+ * Constructs a RADIUS packet with the given code and identifier.
+ *
+ * @param code code
+ * @param identifier identifier
+ */
public RADIUS(byte code, byte identifier) {
this.code = code;
this.identifier = identifier;
}
+ /**
+ * Gets the code.
+ *
+ * @return code
+ */
public byte getCode() {
return this.code;
}
+ /**
+ * Sets the code.
+ *
+ * @param code code
+ */
public void setCode(byte code) {
this.code = code;
}
+ /**
+ * Gets the identifier.
+ *
+ * @return identifier
+ */
public byte getIdentifier() {
return this.identifier;
}
+ /**
+ * Sets the identifier.
+ *
+ * @param identifier identifier
+ */
public void setIdentifier(byte identifier) {
this.identifier = identifier;
}
+ /**
+ * Gets the authenticator.
+ *
+ * @return authenticator
+ */
public byte[] getAuthenticator() {
return this.authenticator;
}
- public void setAuthenticator(byte[] a) {
- this.authenticator = a;
+ /**
+ * Sets the authenticator.
+ *
+ * @param authenticator authenticator
+ */
+ public void setAuthenticator(byte[] authenticator) {
+ this.authenticator = authenticator;
}
+ /**
+ * Generates an authenticator code.
+ *
+ * @return the authenticator
+ */
public byte[] generateAuthCode() {
new SecureRandom().nextBytes(this.authenticator);
return this.authenticator;
}
+ /**
+ * Checks if the packet's code field is valid.
+ *
+ * @return whether the code is valid
+ */
public boolean isValidCode() {
return this.code == RADIUS_CODE_ACCESS_REQUEST ||
this.code == RADIUS_CODE_ACCESS_ACCEPT ||
@@ -104,11 +154,17 @@
this.code == RADIUS_CODE_ACCESS_CHALLENGE;
}
+ /**
+ * Adds a message authenticator to the packet based on the given key.
+ *
+ * @param key key to generate message authenticator
+ * @return the messgae authenticator RADIUS attribute
+ */
public RADIUSAttribute addMessageAuthenticator(String key) {
- /* Message-Authenticator = HMAC-MD5 (Type, Identifier, Length, Request Authenticator, Attributes)
- When the message integrity check is calculated the signature string should be considered to be
- sixteen octets of zero.
- */
+ // Message-Authenticator = HMAC-MD5 (Type, Identifier, Length,
+ // Request Authenticator, Attributes)
+ // When the message integrity check is calculated the signature string
+ // should be considered to be sixteen octets of zero.
byte[] hashOutput = new byte[16];
Arrays.fill(hashOutput, (byte) 0);
@@ -136,6 +192,13 @@
return authAttribute;
}
+ /**
+ * Checks the message authenticator in the packet with one generated from
+ * the given key.
+ *
+ * @param key key to generate message authenticator
+ * @return whether the message authenticators match or not
+ */
public boolean checkMessageAuthenticator(String key) {
byte[] newHash = new byte[16];
Arrays.fill(newHash, (byte) 0);
@@ -156,8 +219,10 @@
}
/**
- * @param message
- * EAP message object to be embedded in the RADIUS EAP-Message attributed
+ * Encapsulates an EAP packet in this RADIUS packet.
+ *
+ * @param message EAP message object to be embedded in the RADIUS
+ * EAP-Message attributed
*/
public void encapsulateMessage(EAP message) {
if (message.length <= MAX_ATTR_VALUE_LENGTH) {
@@ -193,6 +258,8 @@
}
/**
+ * Decapsulates an EAP packet from the RADIUS packet.
+ *
* @return An EAP object containing the reassembled EAP message
*/
public EAP decapsulateMessage() {
@@ -212,8 +279,9 @@
}
/**
- * @param attrType
- * the type field of the required attributes
+ * Gets a list of attributes from the RADIUS packet.
+ *
+ * @param attrType the type field of the required attributes
* @return List of the attributes that matches the type or an empty list if there is none
*/
public ArrayList<RADIUSAttribute> getAttributeList(byte attrType) {
@@ -227,8 +295,9 @@
}
/**
- * @param attrType
- * the type field of the required attribute
+ * Gets an attribute from the RADIUS packet.
+ *
+ * @param attrType the type field of the required attribute
* @return the first attribute that matches the type or null if does not exist
*/
public RADIUSAttribute getAttribute(byte attrType) {
@@ -241,10 +310,10 @@
}
/**
- * @param attrType
- * the type field of the attribute to set
- * @param value
- * value to be set
+ * Sets an attribute in the RADIUS packet.
+ *
+ * @param attrType the type field of the attribute to set
+ * @param value value to be set
* @return reference to the attribute object
*/
public RADIUSAttribute setAttribute(byte attrType, byte[] value) {
@@ -255,6 +324,13 @@
return newAttribute;
}
+ /**
+ * Updates an attribute in the RADIUS packet.
+ *
+ * @param attrType the type field of the attribute to update
+ * @param value the value to update to
+ * @return reference to the attribute object
+ */
public RADIUSAttribute updateAttribute(byte attrType, byte[] value) {
for (int i = 0; i < this.attributes.size(); i++) {
if (this.attributes.get(i).getType() == attrType) {
@@ -268,6 +344,40 @@
return null;
}
+ /**
+ * Deserializer for RADIUS packets.
+ *
+ * @return deserializer
+ */
+ public static Deserializer<RADIUS> deserializer() {
+ return (data, offset, length) -> {
+ checkInput(data, offset, length, RADIUS_MIN_LENGTH);
+
+ final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
+ RADIUS radius = new RADIUS();
+ radius.code = bb.get();
+ radius.identifier = bb.get();
+ radius.length = bb.getShort();
+ bb.get(radius.authenticator, 0, 16);
+
+ checkHeaderLength(length, radius.length);
+
+ int remainingLength = radius.length - RADIUS_MIN_LENGTH;
+ while (remainingLength > 0 && bb.hasRemaining()) {
+
+ RADIUSAttribute attr = new RADIUSAttribute();
+ attr.setType(bb.get());
+ attr.setLength(bb.get());
+ short attrLength = (short) (attr.length & 0xff);
+ attr.value = new byte[attrLength - 2];
+ bb.get(attr.value, 0, attrLength - 2);
+ radius.attributes.add(attr);
+ remainingLength -= attr.length;
+ }
+ return radius;
+ };
+ }
+
@Override
public byte[] serialize() {
final byte[] data = new byte[this.length];
diff --git a/apps/aaa/src/main/java/org/onosproject/aaa/packet/RADIUSAttribute.java b/utils/misc/src/main/java/org/onlab/packet/RADIUSAttribute.java
similarity index 79%
rename from apps/aaa/src/main/java/org/onosproject/aaa/packet/RADIUSAttribute.java
rename to utils/misc/src/main/java/org/onlab/packet/RADIUSAttribute.java
index 5b0fe45..9687e37 100644
--- a/apps/aaa/src/main/java/org/onosproject/aaa/packet/RADIUSAttribute.java
+++ b/utils/misc/src/main/java/org/onlab/packet/RADIUSAttribute.java
@@ -16,16 +16,17 @@
*
*/
-package org.onosproject.aaa.packet;
+package org.onlab.packet;
-import java.nio.ByteBuffer;
-
+/**
+ * An attribute in a RADIUS packet.
+ */
public class RADIUSAttribute {
protected byte type;
protected byte length;
protected byte[] value;
- /* RADIUS attribute types */
+ // RADIUS attribute types
public static final byte RADIUS_ATTR_USERNAME = 1;
public static final byte RADIUS_ATTR_NAS_IP = 4;
public static final byte RADIUS_ATTR_NAS_PORT = 5;
@@ -40,15 +41,30 @@
public static final byte RADIUS_ATTR_MESSAGE_AUTH = 80;
public static final byte RADIUS_ATTR_NAS_PORT_ID = 87;
+ /**
+ * Default constructor.
+ */
public RADIUSAttribute() {
}
+ /**
+ * Constructs a RADIUS attribute with the give type, length and value.
+ *
+ * @param type type
+ * @param length length
+ * @param value value
+ */
public RADIUSAttribute(final byte type, final byte length, final byte[] value) {
this.type = type;
this.length = length;
this.value = value;
}
+ /**
+ * Checks if the attribute type is valid.
+ *
+ * @return whether the type is valid or not
+ */
public boolean isValidType() {
return this.type == RADIUS_ATTR_USERNAME ||
this.type == RADIUS_ATTR_NAS_IP ||
@@ -64,6 +80,8 @@
}
/**
+ * Gets the attribute type.
+ *
* @return the type
*/
public byte getType() {
@@ -71,8 +89,9 @@
}
/**
- * @param type
- * the code to set
+ * Sets the attribute type.
+ *
+ * @param type the code to set
* @return this
*/
public RADIUSAttribute setType(final byte type) {
@@ -81,6 +100,8 @@
}
/**
+ * Gets the attribute length.
+ *
* @return the length
*/
public byte getLength() {
@@ -88,8 +109,9 @@
}
/**
- * @param length
- * the length to set
+ * Sets the attribute length.
+ *
+ * @param length the length to set
* @return this
*/
public RADIUSAttribute setLength(final byte length) {
@@ -98,6 +120,8 @@
}
/**
+ * Gets the attribute value.
+ *
* @return the value
*/
public byte[] getValue() {
@@ -105,8 +129,9 @@
}
/**
- * @param value
- * the data to set
+ * Sets the attribute value.
+ *
+ * @param value the data to set
* @return this
*/
public RADIUSAttribute setValue(final byte[] value) {
@@ -114,12 +139,4 @@
return this;
}
- public byte[] serialize() {
- final byte[] data = new byte[this.length];
- final ByteBuffer bb = ByteBuffer.wrap(data);
- bb.put(this.type);
- bb.put(this.length);
- bb.put(this.value);
- return data;
- }
}