use of org.opendaylight.openflowplugin.libraries.liblldp.Ethernet in project openflowplugin by opendaylight.
the class LLDPDiscoveryUtils method lldpToNodeConnectorRef.
/**
* Returns the encoded in custom TLV for the given lldp.
*
* @param payload lldp payload
* @param useExtraAuthenticatorCheck make it more secure (CVE-2015-1611 CVE-2015-1612)
* @return nodeConnectorId - encoded in custom TLV of given lldp
*/
@SuppressWarnings("checkstyle:IllegalCatch")
public static NodeConnectorRef lldpToNodeConnectorRef(byte[] payload, boolean useExtraAuthenticatorCheck) {
NodeConnectorRef nodeConnectorRef = null;
if (isLLDP(payload)) {
Ethernet ethPkt = new Ethernet();
try {
ethPkt.deserialize(payload, 0, payload.length * NetUtils.NUM_BITS_IN_A_BYTE);
} catch (PacketException e) {
LOG.warn("Failed to decode LLDP packet {}", e);
return nodeConnectorRef;
}
LLDP lldp = (LLDP) ethPkt.getPayload();
try {
NodeId srcNodeId = null;
NodeConnectorId srcNodeConnectorId = null;
final LLDPTLV systemIdTLV = lldp.getSystemNameId();
if (systemIdTLV != null) {
String srcNodeIdString = new String(systemIdTLV.getValue(), Charset.defaultCharset());
srcNodeId = new NodeId(srcNodeIdString);
} else {
throw new Exception("Node id wasn't specified via systemNameId in LLDP packet.");
}
final LLDPTLV nodeConnectorIdLldptlv = lldp.getCustomTLV(LLDPTLV.createPortSubTypeCustomTLVKey());
if (nodeConnectorIdLldptlv != null) {
srcNodeConnectorId = new NodeConnectorId(LLDPTLV.getCustomString(nodeConnectorIdLldptlv.getValue(), nodeConnectorIdLldptlv.getLength()));
} else {
throw new Exception("Node connector wasn't specified via Custom TLV in LLDP packet.");
}
if (useExtraAuthenticatorCheck) {
boolean secure = checkExtraAuthenticator(lldp, srcNodeConnectorId);
if (!secure) {
LOG.warn("SECURITY ALERT: there is probably a LLDP spoofing attack in progress.");
throw new Exception("Attack. LLDP packet with inconsistent extra authenticator field was received.");
}
}
InstanceIdentifier<NodeConnector> srcInstanceId = InstanceIdentifier.builder(Nodes.class).child(Node.class, new NodeKey(srcNodeId)).child(NodeConnector.class, new NodeConnectorKey(srcNodeConnectorId)).toInstance();
nodeConnectorRef = new NodeConnectorRef(srcInstanceId);
} catch (Exception e) {
LOG.debug("Caught exception while parsing out lldp optional and custom fields", e);
}
}
return nodeConnectorRef;
}
use of org.opendaylight.openflowplugin.libraries.liblldp.Ethernet in project openflowplugin by opendaylight.
the class LLDPUtil method buildLldpFrame.
@Nonnull
static byte[] buildLldpFrame(final NodeId nodeId, final NodeConnectorId nodeConnectorId, final MacAddress src, final Long outPortNo, final MacAddress destinationAddress) throws NoSuchAlgorithmException, PacketException {
// Create discovery pkt
LLDP discoveryPkt = new LLDP();
// Create LLDP ChassisID TLV
BigInteger dataPathId = dataPathIdFromNodeId(nodeId);
byte[] cidValue = LLDPTLV.createChassisIDTLVValue(colonize(bigIntegerToPaddedHex(dataPathId)));
LLDPTLV chassisIdTlv = new LLDPTLV();
chassisIdTlv.setType(LLDPTLV.TLVType.ChassisID.getValue());
chassisIdTlv.setType(LLDPTLV.TLVType.ChassisID.getValue()).setLength((short) cidValue.length).setValue(cidValue);
discoveryPkt.setChassisId(chassisIdTlv);
// Create LLDP PortID TL
String hexString = Long.toHexString(outPortNo);
byte[] pidValue = LLDPTLV.createPortIDTLVValue(hexString);
LLDPTLV portIdTlv = new LLDPTLV();
portIdTlv.setType(LLDPTLV.TLVType.PortID.getValue()).setLength((short) pidValue.length).setValue(pidValue);
portIdTlv.setType(LLDPTLV.TLVType.PortID.getValue());
discoveryPkt.setPortId(portIdTlv);
// Create LLDP TTL TLV
byte[] ttl = new byte[] { (byte) 0x13, (byte) 0x37 };
LLDPTLV ttlTlv = new LLDPTLV();
ttlTlv.setType(LLDPTLV.TLVType.TTL.getValue()).setLength((short) ttl.length).setValue(ttl);
discoveryPkt.setTtl(ttlTlv);
// Create LLDP SystemName TLV
byte[] snValue = LLDPTLV.createSystemNameTLVValue(nodeId.getValue());
LLDPTLV systemNameTlv = new LLDPTLV();
systemNameTlv.setType(LLDPTLV.TLVType.SystemName.getValue());
systemNameTlv.setType(LLDPTLV.TLVType.SystemName.getValue()).setLength((short) snValue.length).setValue(snValue);
discoveryPkt.setSystemNameId(systemNameTlv);
// Create LLDP Custom TLV
byte[] customValue = LLDPTLV.createCustomTLVValue(nodeConnectorId.getValue());
LLDPTLV customTlv = new LLDPTLV();
customTlv.setType(LLDPTLV.TLVType.Custom.getValue()).setLength((short) customValue.length).setValue(customValue);
discoveryPkt.addCustomTLV(customTlv);
// Create LLDP CustomSec TLV
byte[] pureValue = getValueForLLDPPacketIntegrityEnsuring(nodeConnectorId);
byte[] customSecValue = LLDPTLV.createSecSubTypeCustomTLVValue(pureValue);
LLDPTLV customSecTlv = new LLDPTLV();
customSecTlv.setType(LLDPTLV.TLVType.Custom.getValue()).setLength((short) customSecValue.length).setValue(customSecValue);
discoveryPkt.addCustomTLV(customSecTlv);
// Create ethernet pkt
byte[] sourceMac = HexEncode.bytesFromHexString(src.getValue());
Ethernet ethPkt = new Ethernet();
ethPkt.setSourceMACAddress(sourceMac).setEtherType(EtherTypes.LLDP.shortValue()).setPayload(discoveryPkt);
if (destinationAddress == null) {
ethPkt.setDestinationMACAddress(LLDP.LLDP_MULTICAST_MAC);
} else {
ethPkt.setDestinationMACAddress(HexEncode.bytesFromHexString(destinationAddress.getValue()));
}
return ethPkt.serialize();
}
Aggregations