Search in sources :

Example 6 with PacketException

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

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

the class AlivenessMonitor method onPacketReceived.

@Override
public void onPacketReceived(PacketReceived packetReceived) {
    Class<? extends PacketInReason> pktInReason = packetReceived.getPacketInReason();
    if (LOG.isTraceEnabled()) {
        LOG.trace("Packet Received {}", packetReceived);
    }
    if (pktInReason == SendToController.class) {
        Packet packetInFormatted;
        byte[] data = packetReceived.getPayload();
        Ethernet res = new Ethernet();
        try {
            packetInFormatted = res.deserialize(data, 0, data.length * NetUtils.NUM_BITS_IN_A_BYTE);
        } catch (PacketException e) {
            LOG.warn("Failed to decode packet: ", e);
            return;
        }
        if (packetInFormatted == null) {
            LOG.warn("Failed to deserialize Received Packet from table {}", packetReceived.getTableId().getValue());
            return;
        }
        Packet objPayload = packetInFormatted.getPayload();
        if (objPayload == null) {
            LOG.trace("Unsupported packet type. Ignoring the packet...");
            return;
        }
        if (LOG.isTraceEnabled()) {
            LOG.trace("onPacketReceived packet: {}, packet class: {}", packetReceived, objPayload.getClass());
        }
        AlivenessProtocolHandler<Packet> livenessProtocolHandler = alivenessProtocolHandlerRegistry.getOpt(Packet.class);
        if (livenessProtocolHandler == null) {
            return;
        }
        String monitorKey = livenessProtocolHandler.handlePacketIn(packetInFormatted.getPayload(), packetReceived);
        if (monitorKey != null) {
            processReceivedMonitorKey(monitorKey);
        } else {
            LOG.debug("No monitorkey associated with received packet");
        }
    }
}
Also used : Packet(org.opendaylight.openflowplugin.libraries.liblldp.Packet) Ethernet(org.opendaylight.genius.mdsalutil.packet.Ethernet) PacketException(org.opendaylight.openflowplugin.libraries.liblldp.PacketException)

Example 8 with PacketException

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

the class AlivenessProtocolHandlerLLDP method startMonitoringTask.

@Override
public void startMonitoringTask(MonitoringInfo monitorInfo) {
    String sourceInterface;
    EndpointType source = monitorInfo.getSource().getEndpointType();
    if (source instanceof Interface) {
        Interface intf = (Interface) source;
        sourceInterface = intf.getInterfaceName();
    } else {
        LOG.warn("Invalid source endpoint. Could not retrieve source interface to send LLDP Packet");
        return;
    }
    // Get Mac Address for the source interface
    org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface interfaceState;
    try {
        interfaceState = getInterfaceFromOperDS(sourceInterface);
    } catch (ReadFailedException e) {
        LOG.error("getInterfaceFromOperDS failed for sourceInterface: {}", sourceInterface, e);
        return;
    }
    Optional<byte[]> optSourceMac = getMacAddress(interfaceState);
    if (!optSourceMac.isPresent()) {
        LOG.error("Could not read mac address for the source interface {} from the Inventory. " + "LLDP packet cannot be send.", sourceInterface);
        return;
    }
    byte[] sourceMac = optSourceMac.get();
    String lowerLayerIf = interfaceState.getLowerLayerIf().get(0);
    NodeConnectorId nodeConnectorId = new NodeConnectorId(lowerLayerIf);
    long nodeId = Long.parseLong(getDpnFromNodeConnectorId(nodeConnectorId));
    long portNum = Long.parseLong(getPortNoFromNodeConnectorId(nodeConnectorId));
    Ethernet ethenetLLDPPacket = makeLLDPPacket(Long.toString(nodeId), portNum, sourceMac, sourceInterface);
    try {
        List<ActionInfo> actions = getInterfaceActions(interfaceState, portNum);
        if (actions.isEmpty()) {
            LOG.error("No interface actions to send packet out over interface {}", sourceInterface);
            return;
        }
        TransmitPacketInput transmitPacketInput = MDSALUtil.getPacketOut(actions, ethenetLLDPPacket.serialize(), nodeId, MDSALUtil.getNodeConnRef(BigInteger.valueOf(nodeId), "0xfffffffd"));
        addErrorLogging(packetProcessingService.transmitPacket(transmitPacketInput), LOG, "transmitPacket() failed: {}", transmitPacketInput);
    } catch (InterruptedException | ExecutionException | PacketException e) {
        LOG.error("Error while sending LLDP Packet", e);
    }
}
Also used : Collections(java.util.Collections) NodeConnectorId(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId) ActionInfo(org.opendaylight.genius.mdsalutil.ActionInfo) PacketException(org.opendaylight.openflowplugin.libraries.liblldp.PacketException) EndpointType(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.endpoint.EndpointType) ExecutionException(java.util.concurrent.ExecutionException) ReadFailedException(org.opendaylight.controller.md.sal.common.api.data.ReadFailedException) Ethernet(org.opendaylight.genius.mdsalutil.packet.Ethernet) TransmitPacketInput(org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput) Interface(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.endpoint.endpoint.type.Interface)

Example 9 with PacketException

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

the class ArpUtilImpl method sendArpResponse.

@Override
public Future<RpcResult<Void>> sendArpResponse(SendArpResponseInput input) {
    LOG.trace("sendArpResponse rpc invoked");
    BigInteger dpnId;
    byte[] payload;
    byte[] srcMac;
    try {
        String interfaceName = input.getInterface();
        GetPortFromInterfaceOutput portResult = getPortFromInterface(interfaceName);
        checkNotNull(portResult);
        dpnId = portResult.getDpid();
        Long portid = portResult.getPortno();
        NodeConnectorRef ref = MDSALUtil.getNodeConnRef(dpnId, portid.toString());
        checkArgument(null != dpnId && !BigInteger.ZERO.equals(dpnId), ArpConstants.DPN_NOT_FOUND_ERROR, interfaceName);
        checkNotNull(ref, ArpConstants.NODE_CONNECTOR_NOT_FOUND_ERROR, interfaceName);
        LOG.trace("sendArpRequest received dpnId {} out interface {}", dpnId, interfaceName);
        byte[] srcIpBytes = getIpAddressBytes(input.getSrcIpaddress());
        byte[] dstIpBytes = getIpAddressBytes(input.getDstIpaddress());
        if (input.getSrcMacaddress() == null) {
            srcMac = portResult.getPhyAddress().getBytes("UTF-8");
        } else {
            String macAddr = input.getSrcMacaddress().getValue();
            srcMac = HexEncode.bytesFromHexString(macAddr);
        }
        byte[] dstMac = NWUtil.parseMacAddress(input.getDstMacaddress().getValue());
        checkNotNull(srcIpBytes, ArpConstants.FAILED_TO_GET_SRC_IP_FOR_INTERFACE, interfaceName);
        payload = ArpPacketUtil.getPayload(ArpConstants.ARP_RESPONSE_OP, srcMac, srcIpBytes, dstMac, dstIpBytes);
        List<Action> actions = getEgressAction(interfaceName);
        sendPacketOutWithActions(dpnId, payload, ref, actions);
        LOG.debug("Sent ARP response for IP {}, from source MAC {} to target MAC {} and target IP {} via dpnId {}", input.getSrcIpaddress().getIpv4Address().getValue(), HexEncode.bytesToHexStringFormat(srcMac), HexEncode.bytesToHexStringFormat(dstMac), input.getDstIpaddress().getIpv4Address().getValue(), dpnId);
    } catch (UnknownHostException | PacketException | InterruptedException | UnsupportedEncodingException | ExecutionException e) {
        LOG.error("failed to send arp response for {}: ", input.getSrcIpaddress(), e);
        return RpcResultBuilder.<Void>failed().withError(ErrorType.APPLICATION, e.getMessage(), e).buildFuture();
    }
    RpcResultBuilder<Void> rpcResultBuilder = RpcResultBuilder.success();
    return Futures.immediateFuture(rpcResultBuilder.build());
}
Also used : NodeConnectorRef(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef) Action(org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action) UnknownHostException(java.net.UnknownHostException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PacketException(org.opendaylight.openflowplugin.libraries.liblldp.PacketException) BigInteger(java.math.BigInteger) GetPortFromInterfaceOutput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetPortFromInterfaceOutput) ExecutionException(java.util.concurrent.ExecutionException)

Example 10 with PacketException

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

the class ArpUtilImpl method onPacketReceived.

@Override
public void onPacketReceived(PacketReceived packetReceived) {
    Class<? extends PacketInReason> pktInReason = packetReceived.getPacketInReason();
    LOG.trace("Packet Received {}", packetReceived);
    if (pktInReason == SendToController.class) {
        try {
            int tableId = packetReceived.getTableId().getValue();
            byte[] data = packetReceived.getPayload();
            Ethernet ethernet = new Ethernet();
            ethernet.deserialize(data, 0, data.length * NetUtils.NUM_BITS_IN_A_BYTE);
            if (ethernet.getEtherType() != ArpConstants.ETH_TYPE_ARP) {
                return;
            }
            Packet pkt = ethernet.getPayload();
            ARP arp = (ARP) pkt;
            InetAddress srcInetAddr = InetAddress.getByAddress(arp.getSenderProtocolAddress());
            InetAddress dstInetAddr = InetAddress.getByAddress(arp.getTargetProtocolAddress());
            byte[] srcMac = ethernet.getSourceMACAddress();
            byte[] dstMac = ethernet.getDestinationMACAddress();
            Metadata metadata = packetReceived.getMatch().getMetadata();
            String interfaceName = getInterfaceName(metadata);
            checkAndFireMacChangedNotification(interfaceName, srcInetAddr, srcMac);
            macsDB.put(interfaceName + "-" + srcInetAddr.getHostAddress(), NWUtil.toStringMacAddress(srcMac));
            if (arp.getOpCode() == ArpConstants.ARP_REQUEST_OP) {
                fireArpReqRecvdNotification(interfaceName, srcInetAddr, srcMac, dstInetAddr, tableId, metadata.getMetadata());
            } else {
                fireArpRespRecvdNotification(interfaceName, srcInetAddr, srcMac, tableId, metadata.getMetadata(), dstInetAddr, dstMac);
            }
            if (macAddrs.get(srcInetAddr.getHostAddress()) != null) {
                threadPool.execute(new MacResponderTask(arp));
            }
        } catch (PacketException | UnknownHostException | InterruptedException | ExecutionException e) {
            LOG.trace("Failed to decode packet", e);
        }
    }
}
Also used : Packet(org.opendaylight.openflowplugin.libraries.liblldp.Packet) UnknownHostException(java.net.UnknownHostException) Metadata(org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Metadata) PacketException(org.opendaylight.openflowplugin.libraries.liblldp.PacketException) Ethernet(org.opendaylight.genius.mdsalutil.packet.Ethernet) ExecutionException(java.util.concurrent.ExecutionException) InetAddress(java.net.InetAddress) ARP(org.opendaylight.genius.mdsalutil.packet.ARP)

Aggregations

PacketException (org.opendaylight.openflowplugin.libraries.liblldp.PacketException)18 Ethernet (org.opendaylight.genius.mdsalutil.packet.Ethernet)10 BigInteger (java.math.BigInteger)6 ExecutionException (java.util.concurrent.ExecutionException)5 UnknownHostException (java.net.UnknownHostException)4 IPv4 (org.opendaylight.genius.mdsalutil.packet.IPv4)4 NodeConnectorRef (org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef)4 InterfaceInfo (org.opendaylight.genius.interfacemanager.globals.InterfaceInfo)3 UDP (org.opendaylight.genius.mdsalutil.packet.UDP)3 BufferException (org.opendaylight.openflowplugin.libraries.liblldp.BufferException)3 Packet (org.opendaylight.openflowplugin.libraries.liblldp.Packet)3 NodeConnectorId (org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 InetAddress (java.net.InetAddress)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 ReadFailedException (org.opendaylight.controller.md.sal.common.api.data.ReadFailedException)2 ActionInfo (org.opendaylight.genius.mdsalutil.ActionInfo)2 ARP (org.opendaylight.genius.mdsalutil.packet.ARP)2 DHCP (org.opendaylight.netvirt.dhcpservice.api.DHCP)2 Action (org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action)2