Search in sources :

Example 1 with LLDPTLV

use of org.opendaylight.openflowplugin.libraries.liblldp.LLDPTLV in project genius by opendaylight.

the class AlivenessProtocolHandlerLLDP method handlePacketIn.

@Override
// TODO remove when 1st  below lldpTlvTypeCur below is removed
@SuppressFBWarnings("DLS_DEAD_LOCAL_STORE")
public String handlePacketIn(LLDP lldpPacket, PacketReceived packetReceived) {
    String tempString = null;
    byte lldpTlvTypeCur;
    // TODO Remove? this seems completely pointless - lldpTlvTypeCur will get overwritten below..
    for (LLDPTLV lldpTlv : lldpPacket.getOptionalTLVList()) {
        lldpTlvTypeCur = lldpTlv.getType();
    }
    for (LLDPTLV lldpTlv : lldpPacket.getCustomTlvList()) {
        lldpTlvTypeCur = lldpTlv.getType();
        if (lldpTlvTypeCur == LLDPTLV.TLVType.Custom.getValue()) {
            tempString = new String(lldpTlv.getValue(), LLDPTLV_CHARSET);
        }
    }
    String interfaceName = null;
    // TODO: Check if the below fields are required
    if (!Strings.isNullOrEmpty(tempString) && tempString.contains("#")) {
        String[] asTmp = tempString.split("#");
        interfaceName = asTmp[0];
        LOG.debug("Custom LLDP Value on received packet: {}", tempString);
    }
    if (!Strings.isNullOrEmpty(interfaceName)) {
        String monitorKey = interfaceName + EtherTypes.LLDP;
        return monitorKey;
    } else {
        LOG.debug("No associated interface found to handle received LLDP Packet");
        return null;
    }
}
Also used : LLDPTLV(org.opendaylight.openflowplugin.libraries.liblldp.LLDPTLV) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 2 with LLDPTLV

use of org.opendaylight.openflowplugin.libraries.liblldp.LLDPTLV 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 3 with LLDPTLV

use of org.opendaylight.openflowplugin.libraries.liblldp.LLDPTLV in project openflowplugin by opendaylight.

the class LLDPDiscoveryUtils method checkExtraAuthenticator.

private static boolean checkExtraAuthenticator(LLDP lldp, NodeConnectorId srcNodeConnectorId) throws NoSuchAlgorithmException, BufferException {
    final LLDPTLV hashLldptlv = lldp.getCustomTLV(LLDPTLV.createSecSubTypeCustomTLVKey());
    boolean secAuthenticatorOk = false;
    if (hashLldptlv != null) {
        byte[] rawTlvValue = hashLldptlv.getValue();
        byte[] lldpCustomSecurityHash = ArrayUtils.subarray(rawTlvValue, 4, rawTlvValue.length);
        byte[] calculatedHash = getValueForLLDPPacketIntegrityEnsuring(srcNodeConnectorId);
        secAuthenticatorOk = Arrays.equals(calculatedHash, lldpCustomSecurityHash);
    } else {
        LOG.debug("Custom security hint wasn't specified via Custom TLV in LLDP packet.");
    }
    return secAuthenticatorOk;
}
Also used : LLDPTLV(org.opendaylight.openflowplugin.libraries.liblldp.LLDPTLV)

Example 4 with LLDPTLV

use of org.opendaylight.openflowplugin.libraries.liblldp.LLDPTLV 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)

Example 5 with LLDPTLV

use of org.opendaylight.openflowplugin.libraries.liblldp.LLDPTLV in project genius by opendaylight.

the class AlivenessProtocolHandlerLLDP method makeLLDPPacket.

public Ethernet makeLLDPPacket(String nodeId, long portNum, byte[] srcMac, String sourceInterface) {
    // Create LLDP TTL TLV
    LLDPTLV lldpTlvTTL = buildLLDTLV(LLDPTLV.TLVType.TTL, new byte[] { (byte) 0, (byte) 120 });
    LLDPTLV lldpTlvChassisId = buildLLDTLV(LLDPTLV.TLVType.ChassisID, LLDPTLV.createChassisIDTLVValue(colonize(StringUtils.leftPad(Long.toHexString(MDSALUtil.getDpnIdFromNodeName(nodeId).longValue()), 16, "0"))));
    LLDPTLV lldpTlvSystemName = buildLLDTLV(TLVType.SystemName, LLDPTLV.createSystemNameTLVValue(nodeId));
    LLDPTLV lldpTlvPortId = buildLLDTLV(TLVType.PortID, LLDPTLV.createPortIDTLVValue(Long.toHexString(portNum)));
    String customValue = sourceInterface + "#" + getPacketId();
    LOG.debug("Sending LLDP packet, custome value {}", customValue);
    LLDPTLV lldpTlvCustom = buildLLDTLV(TLVType.Custom, customValue.getBytes(LLDPTLV_CHARSET));
    @SuppressWarnings("AbbreviationAsWordInName") List<LLDPTLV> lstLLDPTLVCustom = new ArrayList<>();
    lstLLDPTLVCustom.add(lldpTlvCustom);
    LLDP lldpDiscoveryPacket = new LLDP();
    lldpDiscoveryPacket.setChassisId(lldpTlvChassisId).setPortId(lldpTlvPortId).setTtl(lldpTlvTTL).setSystemNameId(lldpTlvSystemName).setOptionalTLVList(lstLLDPTLVCustom);
    byte[] destMac = LLDP.LLDP_MULTICAST_MAC;
    Ethernet ethernetPacket = new Ethernet();
    ethernetPacket.setSourceMACAddress(srcMac).setDestinationMACAddress(destMac).setEtherType(EtherTypes.LLDP.shortValue()).setPayload(lldpDiscoveryPacket);
    return ethernetPacket;
}
Also used : Ethernet(org.opendaylight.genius.mdsalutil.packet.Ethernet) ArrayList(java.util.ArrayList) LLDPTLV(org.opendaylight.openflowplugin.libraries.liblldp.LLDPTLV) LLDP(org.opendaylight.openflowplugin.libraries.liblldp.LLDP)

Aggregations

LLDPTLV (org.opendaylight.openflowplugin.libraries.liblldp.LLDPTLV)5 LLDP (org.opendaylight.openflowplugin.libraries.liblldp.LLDP)3 Ethernet (org.opendaylight.openflowplugin.libraries.liblldp.Ethernet)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 BigInteger (java.math.BigInteger)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ArrayList (java.util.ArrayList)1 Nonnull (javax.annotation.Nonnull)1 Ethernet (org.opendaylight.genius.mdsalutil.packet.Ethernet)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