Search in sources :

Example 1 with UDP

use of net.floodlightcontroller.packet.UDP in project open-kilda by telstra.

the class PathVerificationService method generateVerificationPacket.

public OFPacketOut generateVerificationPacket(IOFSwitch srcSw, OFPort port, IOFSwitch dstSw, boolean sign) {
    try {
        OFPortDesc ofPortDesc = srcSw.getPort(port);
        byte[] chassisId = new byte[] { 4, 0, 0, 0, 0, 0, 0 };
        byte[] portId = new byte[] { 2, 0, 0 };
        byte[] ttlValue = new byte[] { 0, 0x78 };
        byte[] dpidTLVValue = new byte[] { 0x0, 0x26, (byte) 0xe1, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        LLDPTLV dpidTLV = new LLDPTLV().setType((byte) 127).setLength((short) dpidTLVValue.length).setValue(dpidTLVValue);
        byte[] dpidArray = new byte[8];
        ByteBuffer dpidBB = ByteBuffer.wrap(dpidArray);
        ByteBuffer portBB = ByteBuffer.wrap(portId, 1, 2);
        DatapathId dpid = srcSw.getId();
        dpidBB.putLong(dpid.getLong());
        System.arraycopy(dpidArray, 2, chassisId, 1, 6);
        // Set the optionalTLV to the full SwitchID
        System.arraycopy(dpidArray, 0, dpidTLVValue, 4, 8);
        byte[] zeroMac = { 0, 0, 0, 0, 0, 0 };
        byte[] srcMac = ofPortDesc.getHwAddr().getBytes();
        if (Arrays.equals(srcMac, zeroMac)) {
            int portVal = ofPortDesc.getPortNo().getPortNumber();
            // this is a common scenario
            logger.debug("Port {}/{} has zero hardware address: overwrite with lower 6 bytes of dpid", dpid.toString(), portVal);
            System.arraycopy(dpidArray, 2, srcMac, 0, 6);
        }
        portBB.putShort(port.getShortPortNumber());
        VerificationPacket vp = new VerificationPacket();
        vp.setChassisId(new LLDPTLV().setType((byte) 1).setLength((short) chassisId.length).setValue(chassisId));
        vp.setPortId(new LLDPTLV().setType((byte) 2).setLength((short) portId.length).setValue(portId));
        vp.setTtl(new LLDPTLV().setType((byte) 3).setLength((short) ttlValue.length).setValue(ttlValue));
        vp.getOptionalTLVList().add(dpidTLV);
        // Add the controller identifier to the TLV value.
        // vp.getOptionalTLVList().add(controllerTLV);
        // Add T0 based on format from Floodlight LLDP
        long time = System.currentTimeMillis();
        long swLatency = srcSw.getLatency().getValue();
        byte[] timestampTLVValue = ByteBuffer.allocate(Long.SIZE / 8 + 4).put((byte) 0x00).put((byte) 0x26).put((byte) 0xe1).put(// 0x01 is what we'll use to differentiate DPID (0x00) from time (0x01)
        (byte) 0x01).putLong(time + swLatency).array();
        LLDPTLV timestampTLV = new LLDPTLV().setType((byte) 127).setLength((short) timestampTLVValue.length).setValue(timestampTLVValue);
        vp.getOptionalTLVList().add(timestampTLV);
        // Type
        byte[] typeTLVValue = ByteBuffer.allocate(Integer.SIZE / 8 + 4).put((byte) 0x00).put((byte) 0x26).put((byte) 0xe1).put((byte) 0x02).putInt(PathType.ISL.ordinal()).array();
        LLDPTLV typeTLV = new LLDPTLV().setType((byte) 127).setLength((short) typeTLVValue.length).setValue(typeTLVValue);
        vp.getOptionalTLVList().add(typeTLV);
        if (sign) {
            String token = JWT.create().withClaim("dpid", dpid.getLong()).withClaim("ts", time + swLatency).sign(algorithm);
            byte[] tokenBytes = token.getBytes(Charset.forName("UTF-8"));
            byte[] tokenTLVValue = ByteBuffer.allocate(4 + tokenBytes.length).put((byte) 0x00).put((byte) 0x26).put((byte) 0xe1).put((byte) 0x03).put(tokenBytes).array();
            LLDPTLV tokenTLV = new LLDPTLV().setType((byte) 127).setLength((short) tokenTLVValue.length).setValue(tokenTLVValue);
            vp.getOptionalTLVList().add(tokenTLV);
        }
        MacAddress dstMac = MacAddress.of(VERIFICATION_BCAST_PACKET_DST);
        if (dstSw != null) {
            OFPortDesc sw2OfPortDesc = dstSw.getPort(port);
            dstMac = sw2OfPortDesc.getHwAddr();
        }
        Ethernet l2 = new Ethernet().setSourceMACAddress(MacAddress.of(srcMac)).setDestinationMACAddress(dstMac).setEtherType(EthType.IPv4);
        IPv4Address dstIp = IPv4Address.of(VERIFICATION_PACKET_IP_DST);
        if (dstSw != null) {
            dstIp = IPv4Address.of(((InetSocketAddress) dstSw.getInetAddress()).getAddress().getAddress());
        }
        IPv4 l3 = new IPv4().setSourceAddress(IPv4Address.of(((InetSocketAddress) srcSw.getInetAddress()).getAddress().getAddress())).setDestinationAddress(dstIp).setTtl((byte) 64).setProtocol(IpProtocol.UDP);
        UDP l4 = new UDP();
        l4.setSourcePort(TransportPort.of(VERIFICATION_PACKET_UDP_PORT));
        l4.setDestinationPort(TransportPort.of(VERIFICATION_PACKET_UDP_PORT));
        l2.setPayload(l3);
        l3.setPayload(l4);
        l4.setPayload(vp);
        byte[] data = l2.serialize();
        OFPacketOut.Builder pob = srcSw.getOFFactory().buildPacketOut().setBufferId(OFBufferId.NO_BUFFER).setActions(getDiscoveryActions(srcSw, port)).setData(data);
        OFMessageUtils.setInPort(pob, OFPort.CONTROLLER);
        return pob.build();
    } catch (Exception exception) {
        logger.error("error generating verification packet: {}", exception);
    }
    return null;
}
Also used : UDP(net.floodlightcontroller.packet.UDP) InetSocketAddress(java.net.InetSocketAddress) IPv4(net.floodlightcontroller.packet.IPv4) DatapathId(org.projectfloodlight.openflow.types.DatapathId) MacAddress(org.projectfloodlight.openflow.types.MacAddress) ByteBuffer(java.nio.ByteBuffer) IPv4Address(org.projectfloodlight.openflow.types.IPv4Address) OFPacketOut(org.projectfloodlight.openflow.protocol.OFPacketOut) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JWTVerificationException(com.auth0.jwt.exceptions.JWTVerificationException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) FloodlightModuleException(net.floodlightcontroller.core.module.FloodlightModuleException) OFPortDesc(org.projectfloodlight.openflow.protocol.OFPortDesc) OFPortDescPropEthernet(org.projectfloodlight.openflow.protocol.OFPortDescPropEthernet) Ethernet(net.floodlightcontroller.packet.Ethernet) LLDPTLV(net.floodlightcontroller.packet.LLDPTLV)

Example 2 with UDP

use of net.floodlightcontroller.packet.UDP in project open-kilda by telstra.

the class PacketFactory method DhcpDiscoveryRequestEthernet.

/**
 * Generates a DHCP request Ethernet frame.
 * @param hostMac The host MAC address of for the request.
 * @returnAn An Ethernet frame that contains a DHCP request packet.
 */
public static Ethernet DhcpDiscoveryRequestEthernet(MacAddress hostMac) {
    List<DHCPOption> optionList = new ArrayList<DHCPOption>();
    byte[] requestValue = new byte[4];
    requestValue[0] = requestValue[1] = requestValue[2] = requestValue[3] = 0;
    DHCPOption requestOption = new DHCPOption().setCode(DHCP.DHCPOptionCode.OptionCode_RequestedIP.getValue()).setLength((byte) 4).setData(requestValue);
    byte[] msgTypeValue = new byte[1];
    // DHCP request
    msgTypeValue[0] = 1;
    DHCPOption msgTypeOption = new DHCPOption().setCode(DHCP.DHCPOptionCode.OptionCode_MessageType.getValue()).setLength((byte) 1).setData(msgTypeValue);
    byte[] reqParamValue = new byte[4];
    // subnet mask
    reqParamValue[0] = 1;
    // Router
    reqParamValue[1] = 3;
    // Domain Name Server
    reqParamValue[2] = 6;
    // NTP Server
    reqParamValue[3] = 42;
    DHCPOption reqParamOption = new DHCPOption().setCode(DHCP.DHCPOptionCode.OptionCode_RequestedParameters.getValue()).setLength((byte) 4).setData(reqParamValue);
    byte[] clientIdValue = new byte[7];
    // Ethernet
    clientIdValue[0] = 1;
    System.arraycopy(hostMac.getBytes(), 0, clientIdValue, 1, 6);
    DHCPOption clientIdOption = new DHCPOption().setCode(DHCP.DHCPOptionCode.OptionCode_ClientID.getValue()).setLength((byte) 7).setData(clientIdValue);
    DHCPOption endOption = new DHCPOption().setCode(DHCP.DHCPOptionCode.OptionCode_END.getValue()).setLength((byte) 0).setData(null);
    optionList.add(requestOption);
    optionList.add(msgTypeOption);
    optionList.add(reqParamOption);
    optionList.add(clientIdOption);
    optionList.add(endOption);
    Ethernet requestPacket = new Ethernet();
    requestPacket.setSourceMACAddress(hostMac.getBytes()).setDestinationMACAddress(broadcastMac).setEtherType(EthType.IPv4).setPayload(new IPv4().setVersion((byte) 4).setDiffServ((byte) 0).setIdentification((short) 100).setFlags((byte) 0).setFragmentOffset((short) 0).setTtl((byte) 250).setProtocol(IpProtocol.UDP).setChecksum((short) 0).setSourceAddress(0).setDestinationAddress(broadcastIp).setPayload(new UDP().setSourcePort(UDP.DHCP_CLIENT_PORT).setDestinationPort(UDP.DHCP_SERVER_PORT).setChecksum((short) 0).setPayload(new DHCP().setOpCode(DHCP.OPCODE_REQUEST).setHardwareType(DHCP.HWTYPE_ETHERNET).setHardwareAddressLength((byte) 6).setHops((byte) 0).setTransactionId(0x00003d1d).setSeconds((short) 0).setFlags((short) 0).setClientIPAddress(IPv4Address.NONE).setYourIPAddress(IPv4Address.NONE).setServerIPAddress(IPv4Address.NONE).setGatewayIPAddress(IPv4Address.NONE).setClientHardwareAddress(hostMac).setOptions(optionList))));
    return requestPacket;
}
Also used : UDP(net.floodlightcontroller.packet.UDP) DHCPOption(net.floodlightcontroller.packet.DHCPOption) Ethernet(net.floodlightcontroller.packet.Ethernet) IPv4(net.floodlightcontroller.packet.IPv4) ArrayList(java.util.ArrayList) DHCP(net.floodlightcontroller.packet.DHCP)

Example 3 with UDP

use of net.floodlightcontroller.packet.UDP in project open-kilda by telstra.

the class PathVerificationPacketInTest method getPacket.

protected IPacket getPacket() {
    UDP udp = new UDP().setDestinationPort(TransportPort.of(PathVerificationService.VERIFICATION_PACKET_UDP_PORT)).setSourcePort(TransportPort.of(PathVerificationService.VERIFICATION_PACKET_UDP_PORT));
    VerificationPacket verificationPacket = new VerificationPacket().setChassisId(new LLDPTLV().setType((byte) 1).setLength((short) 7).setValue(new byte[] { 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 })).setPortId(new LLDPTLV().setType((byte) 2).setLength((short) 3).setValue(new byte[] { 0x02, 0x00, 0x01 })).setTtl(new LLDPTLV().setType((byte) 3).setLength((short) 2).setValue(new byte[] { 0x00, 0x78 }));
    udp.setPayload(new Data(verificationPacket.serialize()));
    IPv4 ip = new IPv4().setSourceAddress("192.168.0.1").setDestinationAddress(PathVerificationService.VERIFICATION_PACKET_IP_DST).setProtocol(IpProtocol.UDP);
    Ethernet eth = new Ethernet().setDestinationMACAddress("AA:BB:CC:DD:EE:FF").setSourceMACAddress("11:22:33:44:55:66").setEtherType(EthType.IPv4);
    eth.setPayload(ip);
    ip.setPayload(udp);
    return eth;
}
Also used : UDP(net.floodlightcontroller.packet.UDP) IPv4(net.floodlightcontroller.packet.IPv4) Ethernet(net.floodlightcontroller.packet.Ethernet) Data(net.floodlightcontroller.packet.Data) LLDPTLV(net.floodlightcontroller.packet.LLDPTLV)

Aggregations

Ethernet (net.floodlightcontroller.packet.Ethernet)3 IPv4 (net.floodlightcontroller.packet.IPv4)3 UDP (net.floodlightcontroller.packet.UDP)3 LLDPTLV (net.floodlightcontroller.packet.LLDPTLV)2 JWTVerificationException (com.auth0.jwt.exceptions.JWTVerificationException)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InetSocketAddress (java.net.InetSocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 FloodlightModuleException (net.floodlightcontroller.core.module.FloodlightModuleException)1 DHCP (net.floodlightcontroller.packet.DHCP)1 DHCPOption (net.floodlightcontroller.packet.DHCPOption)1 Data (net.floodlightcontroller.packet.Data)1 OFPacketOut (org.projectfloodlight.openflow.protocol.OFPacketOut)1 OFPortDesc (org.projectfloodlight.openflow.protocol.OFPortDesc)1 OFPortDescPropEthernet (org.projectfloodlight.openflow.protocol.OFPortDescPropEthernet)1 DatapathId (org.projectfloodlight.openflow.types.DatapathId)1 IPv4Address (org.projectfloodlight.openflow.types.IPv4Address)1 MacAddress (org.projectfloodlight.openflow.types.MacAddress)1