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/src/main/java/org/onosproject/aaa/AAA.java b/src/main/java/org/onosproject/aaa/AAA.java
index 9168017..5023b54 100644
--- a/src/main/java/org/onosproject/aaa/AAA.java
+++ b/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/src/main/java/org/onosproject/aaa/packet/EAP.java b/src/main/java/org/onosproject/aaa/packet/EAP.java
deleted file mode 100644
index ed2718e..0000000
--- a/src/main/java/org/onosproject/aaa/packet/EAP.java
+++ /dev/null
@@ -1,226 +0,0 @@
-/*
- *
- * * 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.onosproject.aaa.packet;
-
-import org.onlab.packet.BasePacket;
-import org.onlab.packet.IPacket;
-
-import java.nio.ByteBuffer;
-
-
-/**
- *
- */
-public class EAP extends BasePacket {
- 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 */
- 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 */
- public static final byte ATTR_IDENTITY = 0x1;
- public static final byte ATTR_NOTIFICATION = 0x2;
- public static final byte ATTR_NAK = 0x3;
- public static final byte ATTR_MD5 = 0x4;
- public static final byte ATTR_OTP = 0x5;
- public static final byte ATTR_GTC = 0x6;
- public static final byte ATTR_TLS = 0xd;
-
- protected byte code;
- protected byte identifier;
- protected short length;
- protected byte type;
- protected byte[] data;
-
-
- /**
- * Get the EAP code.
- * @return EAP code
- */
- public byte getCode() {
- return this.code;
- }
-
-
- /**
- * Set the EAP code.
- * @param code EAP code
- * @return this
- */
- public EAP setCode(final byte code) {
- this.code = code;
- return this;
- }
-
- /**
- * Get the EAP identifier.
- * @return EAP identifier
- */
- public byte getIdentifier() {
- return this.identifier;
- }
-
- /**
- * Set the EAP identifier.
- * @param identifier
- * @return this
- */
- public EAP setIdentifier(final byte identifier) {
- this.identifier = identifier;
- return this;
- }
-
- /**
- * Get the get packet length.
- * @return packet length
- */
- public short getLength() {
- return this.length;
- }
-
- /**
- * Set the packet length.
- * @param length packet length
- * @return this
- */
- public EAP setLength(final short length) {
- this.length = length;
- return this;
- }
-
- /**
- * Get the data type.
- * @return data type
- */
- public byte getDataType() {
- return this.type;
- }
-
- /**
- * Set the data type.
- * @param type data type
- * @return this
- */
- public EAP setDataType(final byte type) {
- this.type = type;
- return this;
- }
-
- /**
- * Get the EAP data.
- * @return EAP data
- */
- public byte[] getData() {
- return this.data;
- }
-
- /**
- * Set the EAP data.
- * @param data EAP data to be set
- * @return this
- */
- public EAP setData(final byte[] data) {
- this.data = data;
- return this;
- }
-
- /**
- * Default EAP constructor that set the EAP code to 0.
- */
- public EAP() {
- this.code = 0;
- }
-
- /**
- * EAP constructor that initially sets all fields.
- * @param code EAP code
- * @param identifier EAP identifier
- * @param type packet type
- * @param data EAP data
- */
- public EAP(byte code, byte identifier, byte type, byte[] data) {
- this.code = code;
- this.identifier = identifier;
- if (this.code == REQUEST || this.code == RESPONSE) {
- this.length = (short) (5 + (data == null ? 0 : data.length));
- this.type = type;
- } else {
- this.length = (short) (4 + (data == null ? 0 : data.length));
- }
- this.data = data;
- }
-
- /**
- * Serializes the packet, based on the code/type using the payload
- * to compute its length.
- * @return the serialized payload
- */
- @Override
- public byte[] serialize() {
- final byte[] data = new byte[this.length];
-
- final ByteBuffer bb = ByteBuffer.wrap(data);
- bb.put(this.code);
- bb.put(this.identifier);
- bb.putShort(this.length);
- if (this.code == REQUEST || this.code == RESPONSE) {
- bb.put(this.type);
- }
- if (this.data != null) {
- bb.put(this.data);
- }
- return data;
- }
-
- @Override
- public IPacket deserialize(final byte[] data, final int offset,
- final int length) {
- final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
- this.code = bb.get();
- this.identifier = bb.get();
- this.length = bb.getShort();
-
- int dataLength;
- if (this.code == REQUEST || this.code == RESPONSE) {
- this.type = bb.get();
- dataLength = this.length - 5;
- } else {
- dataLength = this.length - 4;
- }
- this.data = new byte[dataLength];
- bb.get(this.data);
- return this;
- }
-
- @Override
- public int hashCode() {
- final int prime = 3889;
- int result = super.hashCode();
- result = prime * result + this.code;
- result = prime * result + this.identifier;
- result = prime * result + this.length;
- result = prime * result + this.type;
- return result;
- }
-}
diff --git a/src/main/java/org/onosproject/aaa/packet/EAPEthernet.java b/src/main/java/org/onosproject/aaa/packet/EAPEthernet.java
deleted file mode 100644
index f975c15..0000000
--- a/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/src/main/java/org/onosproject/aaa/packet/EAPOL.java b/src/main/java/org/onosproject/aaa/packet/EAPOL.java
deleted file mode 100644
index ac11dcc..0000000
--- a/src/main/java/org/onosproject/aaa/packet/EAPOL.java
+++ /dev/null
@@ -1,228 +0,0 @@
-/*
- *
- * * 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.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;
-
-import java.nio.ByteBuffer;
-
-import static org.onlab.packet.PacketUtils.checkInput;
-
-/**
- *
- */
-public class EAPOL extends BasePacket {
-
- private byte version = 0x01;
- private byte eapolType;
- private short packetLength;
-
- /* 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
- });
-
-
- /**
- * Get version.
- * @return version
- */
- public byte getVersion() {
- return this.version;
- }
-
- /**
- * Set version.
- * @param version EAPOL version
- * @return this
- */
- public EAPOL setVersion(final byte version) {
- this.version = version;
- return this;
- }
-
- /**
- * Get type.
- * @return EAPOL type
- */
- public byte getEapolType() {
- return this.eapolType;
- }
-
- /**
- * Set EAPOL type.
- * @param eapolType EAPOL type
- * @return this
- */
- public EAPOL setEapolType(final byte eapolType) {
- this.eapolType = eapolType;
- return this;
- }
-
- /**
- * Get packet length.
- * @return packet length
- */
- public short getPacketLength() {
- return this.packetLength;
- }
-
- /**
- * Set 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;
- }
-
- /**
- *
- * @param dstMac
- * @param srcMac
- * @param eapolType
- * @param eap
- * @return Ethernet frame
- */
- 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);
-
- 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) {
- //deserialize the EAP Payload
- eapol.payload = new EAP();
-
- eapol.payload = eapol.payload.deserialize(data, bb.position(), length - 4);
- 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;
- }
-
-
-}
-
diff --git a/src/main/java/org/onosproject/aaa/packet/RADIUS.java b/src/main/java/org/onosproject/aaa/packet/RADIUS.java
deleted file mode 100644
index 753490b..0000000
--- a/src/main/java/org/onosproject/aaa/packet/RADIUS.java
+++ /dev/null
@@ -1,313 +0,0 @@
-/*
- *
- * * 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.onosproject.aaa.packet;
-
-import org.onlab.packet.BasePacket;
-import org.onlab.packet.IPacket;
-import org.slf4j.Logger;
-
-import javax.crypto.Mac;
-import javax.crypto.spec.SecretKeySpec;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.nio.ByteBuffer;
-import java.security.SecureRandom;
-import java.util.ArrayList;
-import java.util.Arrays;
-
-import static org.slf4j.LoggerFactory.getLogger;
-
-/**
- *
- */
-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<>();
-
- /* 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 */
- 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;
- public static final byte RADIUS_CODE_ACCOUNTING_REQUEST = 0x04;
- public static final byte RADIUS_CODE_ACCOUNTING_RESPONSE = 0x05;
- public static final byte RADIUS_CODE_ACCESS_CHALLENGE = 0x0b;
-
- private final Logger log = getLogger(getClass());
-
- public RADIUS() {
- }
-
- public RADIUS(byte code, byte identifier) {
- this.code = code;
- this.identifier = identifier;
- }
-
- public byte getCode() {
- return this.code;
- }
-
- public void setCode(byte code) {
- this.code = code;
- }
-
- public byte getIdentifier() {
- return this.identifier;
- }
-
- public void setIdentifier(byte identifier) {
- this.identifier = identifier;
- }
-
- public byte[] getAuthenticator() {
- return this.authenticator;
- }
-
- public void setAuthenticator(byte[] a) {
- this.authenticator = a;
- }
-
- public byte[] generateAuthCode() {
- new SecureRandom().nextBytes(this.authenticator);
- return this.authenticator;
- }
-
- public boolean isValidCode() {
- return this.code == RADIUS_CODE_ACCESS_REQUEST ||
- this.code == RADIUS_CODE_ACCESS_ACCEPT ||
- this.code == RADIUS_CODE_ACCESS_REJECT ||
- this.code == RADIUS_CODE_ACCOUNTING_REQUEST ||
- this.code == RADIUS_CODE_ACCOUNTING_RESPONSE ||
- this.code == RADIUS_CODE_ACCESS_CHALLENGE;
- }
-
- 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.
- */
- byte[] hashOutput = new byte[16];
- Arrays.fill(hashOutput, (byte) 0);
-
- RADIUSAttribute authAttribute = this.getAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH);
- if (authAttribute != null) {
- // If Message-Authenticator was already present, override it
- this.log.warn("Attempted to add duplicate Message-Authenticator");
- authAttribute = this.updateAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH, hashOutput);
- } else {
- // Else generate a new attribute padded with zeroes
- authAttribute = this.setAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH, hashOutput);
- }
- // Calculate the MD5 HMAC based on the message
- try {
- SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "HmacMD5");
- Mac mac = Mac.getInstance("HmacMD5");
- mac.init(keySpec);
- hashOutput = mac.doFinal(this.serialize());
- // Update HMAC in Message-Authenticator
- authAttribute = this.updateAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH, hashOutput);
- } catch (Exception e) {
- this.log.error("Failed to generate message authenticator: {}", e.getMessage());
- }
-
- return authAttribute;
- }
-
- public boolean checkMessageAuthenticator(String key) {
- byte[] newHash = new byte[16];
- Arrays.fill(newHash, (byte) 0);
- byte[] messageAuthenticator = this.getAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH).getValue();
- this.updateAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH, newHash);
- // Calculate the MD5 HMAC based on the message
- try {
- SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "HmacMD5");
- Mac mac = Mac.getInstance("HmacMD5");
- mac.init(keySpec);
- newHash = mac.doFinal(this.serialize());
- } catch (Exception e) {
- log.error("Failed to generate message authenticator: {}", e.getMessage());
- }
- this.updateAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH, messageAuthenticator);
- // Compare the calculated Message-Authenticator with the one in the message
- return Arrays.equals(newHash, messageAuthenticator);
- }
-
- /**
- * @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) {
- // Use the regular serialization method as it fits into one EAP-Message attribute
- this.setAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE,
- message.serialize());
- } else {
- // Segment the message into chucks and embed them in several EAP-Message attributes
- short remainingLength = message.length;
- byte[] messageBuffer = message.serialize();
- final ByteBuffer bb = ByteBuffer.wrap(messageBuffer);
- while (bb.hasRemaining()) {
- byte[] messageAttributeData;
- if (remainingLength > MAX_ATTR_VALUE_LENGTH) {
- // The remaining data is still too long to fit into one attribute, keep going
- messageAttributeData = new byte[MAX_ATTR_VALUE_LENGTH];
- bb.get(messageAttributeData, 0, MAX_ATTR_VALUE_LENGTH);
- remainingLength -= MAX_ATTR_VALUE_LENGTH;
- } else {
- // The remaining data fits, this will be the last chunk
- messageAttributeData = new byte[remainingLength];
- bb.get(messageAttributeData, 0, remainingLength);
- }
- this.attributes.add(new RADIUSAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE,
- (byte) (messageAttributeData.length + 2), messageAttributeData));
-
- // Adding the size of the data to the total RADIUS length
- this.length += (short) (messageAttributeData.length & 0xFF);
- // Adding the size of the overhead attribute type and length
- this.length += 2;
- }
- }
- }
-
- /**
- * @return An EAP object containing the reassembled EAP message
- */
- public EAP decapsulateMessage() {
- EAP message = new EAP();
- ByteArrayOutputStream messageStream = new ByteArrayOutputStream();
- // Iterating through EAP-Message attributes to concatenate their value
- for (RADIUSAttribute ra : this.getAttributeList(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE)) {
- try {
- messageStream.write(ra.getValue());
- } catch (IOException e) {
- log.error("Error while reassembling EAP message: {}", e.getMessage());
- }
- }
- // Assembling EAP object from the concatenated stream
- message.deserialize(messageStream.toByteArray(), 0, messageStream.size());
- return message;
- }
-
- /**
- * @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) {
- ArrayList<RADIUSAttribute> attrList = new ArrayList<>();
- for (int i = 0; i < this.attributes.size(); i++) {
- if (this.attributes.get(i).getType() == attrType) {
- attrList.add(this.attributes.get(i));
- }
- }
- return attrList;
- }
-
- /**
- * @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) {
- for (int i = 0; i < this.attributes.size(); i++) {
- if (this.attributes.get(i).getType() == attrType) {
- return this.attributes.get(i);
- }
- }
- return null;
- }
-
- /**
- * @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) {
- byte attrLength = (byte) (value.length + 2);
- RADIUSAttribute newAttribute = new RADIUSAttribute(attrType, attrLength, value);
- this.attributes.add(newAttribute);
- this.length += (short) (attrLength & 0xFF);
- return newAttribute;
- }
-
- public RADIUSAttribute updateAttribute(byte attrType, byte[] value) {
- for (int i = 0; i < this.attributes.size(); i++) {
- if (this.attributes.get(i).getType() == attrType) {
- this.length -= (short) (this.attributes.get(i).getLength() & 0xFF);
- RADIUSAttribute newAttr = new RADIUSAttribute(attrType, (byte) (value.length + 2), value);
- this.attributes.set(i, newAttr);
- this.length += (short) (newAttr.getLength() & 0xFF);
- return newAttr;
- }
- }
- return null;
- }
-
- @Override
- public byte[] serialize() {
- final byte[] data = new byte[this.length];
- final ByteBuffer bb = ByteBuffer.wrap(data);
-
- bb.put(this.code);
- bb.put(this.identifier);
- bb.putShort(this.length);
- bb.put(this.authenticator);
- for (int i = 0; i < this.attributes.size(); i++) {
- RADIUSAttribute attr = this.attributes.get(i);
- bb.put(attr.getType());
- bb.put(attr.getLength());
- bb.put(attr.getValue());
- }
-
- return data;
- }
-
- @Override
- public IPacket deserialize(final byte[] data, final int offset,
- final int length) {
- final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
- this.code = bb.get();
- this.identifier = bb.get();
- this.length = bb.getShort();
- bb.get(this.authenticator, 0, 16);
-
- int remainingLength = this.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);
- this.attributes.add(attr);
- remainingLength -= attr.length;
- }
- return this;
- }
-
-}
diff --git a/src/main/java/org/onosproject/aaa/packet/RADIUSAttribute.java b/src/main/java/org/onosproject/aaa/packet/RADIUSAttribute.java
deleted file mode 100644
index 5b0fe45..0000000
--- a/src/main/java/org/onosproject/aaa/packet/RADIUSAttribute.java
+++ /dev/null
@@ -1,125 +0,0 @@
-/*
- *
- * * 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.onosproject.aaa.packet;
-
-import java.nio.ByteBuffer;
-
-public class RADIUSAttribute {
- protected byte type;
- protected byte length;
- protected byte[] value;
-
- /* 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;
- public static final byte RADIUS_ATTR_FRAMED_MTU = 12;
- public static final byte RADIUS_ATTR_STATE = 24;
- public static final byte RADIUS_ATTR_VENDOR_SPECIFIC = 26;
- public static final byte RADIUS_ATTR_CALLING_STATION_ID = 31;
- public static final byte RADIUS_ATTR_NAS_ID = 32;
- public static final byte RADIUS_ATTR_ACCT_SESSION_ID = 44;
- public static final byte RADIUS_ATTR_NAS_PORT_TYPE = 61;
- public static final byte RADIUS_ATTR_EAP_MESSAGE = 79;
- public static final byte RADIUS_ATTR_MESSAGE_AUTH = 80;
- public static final byte RADIUS_ATTR_NAS_PORT_ID = 87;
-
- public RADIUSAttribute() {
- }
-
- public RADIUSAttribute(final byte type, final byte length, final byte[] value) {
- this.type = type;
- this.length = length;
- this.value = value;
- }
-
- public boolean isValidType() {
- return this.type == RADIUS_ATTR_USERNAME ||
- this.type == RADIUS_ATTR_NAS_IP ||
- this.type == RADIUS_ATTR_NAS_PORT ||
- this.type == RADIUS_ATTR_VENDOR_SPECIFIC ||
- this.type == RADIUS_ATTR_CALLING_STATION_ID ||
- this.type == RADIUS_ATTR_NAS_ID ||
- this.type == RADIUS_ATTR_ACCT_SESSION_ID ||
- this.type == RADIUS_ATTR_NAS_PORT_TYPE ||
- this.type == RADIUS_ATTR_EAP_MESSAGE ||
- this.type == RADIUS_ATTR_MESSAGE_AUTH ||
- this.type == RADIUS_ATTR_NAS_PORT_ID;
- }
-
- /**
- * @return the type
- */
- public byte getType() {
- return this.type;
- }
-
- /**
- * @param type
- * the code to set
- * @return this
- */
- public RADIUSAttribute setType(final byte type) {
- this.type = type;
- return this;
- }
-
- /**
- * @return the length
- */
- public byte getLength() {
- return this.length;
- }
-
- /**
- * @param length
- * the length to set
- * @return this
- */
- public RADIUSAttribute setLength(final byte length) {
- this.length = length;
- return this;
- }
-
- /**
- * @return the value
- */
- public byte[] getValue() {
- return this.value;
- }
-
- /**
- * @param value
- * the data to set
- * @return this
- */
- public RADIUSAttribute setValue(final byte[] value) {
- this.value = value;
- 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;
- }
-}