Search in sources :

Example 1 with Ethernet

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;
}
Also used : NodeConnectorRef(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef) NodeConnector(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector) NodeConnectorId(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId) PacketException(org.opendaylight.openflowplugin.libraries.liblldp.PacketException) BufferException(org.opendaylight.openflowplugin.libraries.liblldp.BufferException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) PacketException(org.opendaylight.openflowplugin.libraries.liblldp.PacketException) Nodes(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes) LLDP(org.opendaylight.openflowplugin.libraries.liblldp.LLDP) Ethernet(org.opendaylight.openflowplugin.libraries.liblldp.Ethernet) NodeId(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId) NodeKey(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey) NodeConnectorKey(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey) LLDPTLV(org.opendaylight.openflowplugin.libraries.liblldp.LLDPTLV)

Example 2 with Ethernet

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();
}
Also used : Ethernet(org.opendaylight.openflowplugin.libraries.liblldp.Ethernet) BigInteger(java.math.BigInteger) LLDP(org.opendaylight.openflowplugin.libraries.liblldp.LLDP) LLDPTLV(org.opendaylight.openflowplugin.libraries.liblldp.LLDPTLV) Nonnull(javax.annotation.Nonnull)

Aggregations

Ethernet (org.opendaylight.openflowplugin.libraries.liblldp.Ethernet)2 LLDP (org.opendaylight.openflowplugin.libraries.liblldp.LLDP)2 LLDPTLV (org.opendaylight.openflowplugin.libraries.liblldp.LLDPTLV)2 BigInteger (java.math.BigInteger)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Nonnull (javax.annotation.Nonnull)1 BufferException (org.opendaylight.openflowplugin.libraries.liblldp.BufferException)1 PacketException (org.opendaylight.openflowplugin.libraries.liblldp.PacketException)1 NodeConnectorId (org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId)1 NodeConnectorRef (org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef)1 NodeId (org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId)1 Nodes (org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes)1 NodeConnector (org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector)1 NodeConnectorKey (org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey)1 NodeKey (org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey)1