Search in sources :

Example 26 with IPv4

use of org.onlab.packet.IPv4 in project onos by opennetworkinglab.

the class OpenstackRoutingSnatHandler method setStatelessSnatDownstreamRules.

private void setStatelessSnatDownstreamRules(InstancePort srcInstPort, String segmentId, Type networkType, IpAddress externalIp, ExternalPeerRouter externalPeerRouter, TpPort patPort, InboundPacket packetIn) {
    IPv4 iPacket = (IPv4) packetIn.parsed().getPayload();
    IpAddress internalIp = IpAddress.valueOf(iPacket.getSourceAddress());
    TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder().matchEthType(Ethernet.TYPE_IPV4).matchIPProtocol(iPacket.getProtocol()).matchIPDst(IpPrefix.valueOf(externalIp.getIp4Address(), VM_PREFIX)).matchIPSrc(IpPrefix.valueOf(iPacket.getDestinationAddress(), VM_PREFIX));
    TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder().setEthDst(packetIn.parsed().getSourceMAC()).setIpDst(internalIp);
    if (!externalPeerRouter.vlanId().equals(VlanId.NONE)) {
        sBuilder.matchVlanId(externalPeerRouter.vlanId());
        tBuilder.popVlan();
    }
    switch(networkType) {
        case VXLAN:
        case GRE:
        case GENEVE:
            tBuilder.setTunnelId(Long.parseLong(segmentId));
            break;
        case VLAN:
            tBuilder.pushVlan().setVlanId(VlanId.vlanId(segmentId));
            break;
        default:
            final String error = String.format("%s %s", ERR_UNSUPPORTED_NET_TYPE, networkType.toString());
            throw new IllegalStateException(error);
    }
    switch(iPacket.getProtocol()) {
        case IPv4.PROTOCOL_TCP:
            TCP tcpPacket = (TCP) iPacket.getPayload();
            sBuilder.matchTcpSrc(TpPort.tpPort(tcpPacket.getDestinationPort())).matchTcpDst(patPort);
            tBuilder.setTcpDst(TpPort.tpPort(tcpPacket.getSourcePort()));
            break;
        case IPv4.PROTOCOL_UDP:
            UDP udpPacket = (UDP) iPacket.getPayload();
            sBuilder.matchUdpSrc(TpPort.tpPort(udpPacket.getDestinationPort())).matchUdpDst(patPort);
            tBuilder.setUdpDst(TpPort.tpPort(udpPacket.getSourcePort()));
            break;
        default:
            break;
    }
    OpenstackNode srcNode = osNodeService.node(srcInstPort.deviceId());
    osNodeService.completeNodes(GATEWAY).forEach(gNode -> {
        TrafficTreatment treatment = getDownstreamTreatment(networkType, tBuilder, gNode, srcNode);
        osFlowRuleService.setRule(appId, gNode.intgBridge(), sBuilder.build(), treatment, PRIORITY_SNAT_RULE, GW_COMMON_TABLE, true);
    });
}
Also used : TCP(org.onlab.packet.TCP) UDP(org.onlab.packet.UDP) IPv4(org.onlab.packet.IPv4) TrafficSelector(org.onosproject.net.flow.TrafficSelector) DefaultTrafficSelector(org.onosproject.net.flow.DefaultTrafficSelector) IpAddress(org.onlab.packet.IpAddress) OpenstackNode(org.onosproject.openstacknode.api.OpenstackNode) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment)

Example 27 with IPv4

use of org.onlab.packet.IPv4 in project onos by opennetworkinglab.

the class OpenstackRemoveAclCommand method doExecute.

@Override
protected void doExecute() {
    OpenstackFlowRuleService flowRuleService = get(OpenstackFlowRuleService.class);
    CoreService coreService = get(CoreService.class);
    ApplicationId appId = coreService.getAppId(OPENSTACK_NETWORKING_APP_ID);
    InstancePortService instancePortService = get(InstancePortService.class);
    IpAddress srcIpAddress = null;
    IpAddress dstIpAddress = null;
    try {
        srcIpAddress = IpAddress.valueOf(srcIpStr);
        dstIpAddress = IpAddress.valueOf(dstIpStr);
    } catch (IllegalArgumentException e) {
        log.error("IllegalArgumentException occurred because of {}", e);
        return;
    }
    TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder().matchEthType(Ethernet.TYPE_IPV4).matchIPSrc(srcIpAddress.toIpPrefix()).matchIPDst(dstIpAddress.toIpPrefix());
    TrafficTreatment treatment = DefaultTrafficTreatment.builder().drop().build();
    if (srcPort != 0 || dstPort != 0) {
        sBuilder.matchIPProtocol(IPv4.PROTOCOL_TCP);
        if (srcPort != 0) {
            sBuilder.matchTcpSrc(TpPort.tpPort(srcPort));
        }
        if (dstPort != 0) {
            sBuilder.matchTcpDst(TpPort.tpPort(dstPort));
        }
    }
    log.info("Deny the packet from srcIp: {}, dstPort: {} to dstIp: {}, dstPort: {}", srcIpAddress.toString(), srcPort, dstIpAddress.toString(), dstPort);
    Optional<InstancePort> instancePort = instancePortService.instancePorts().stream().filter(port -> port.ipAddress().toString().equals(dstIpStr)).findAny();
    if (!instancePort.isPresent()) {
        log.info("Instance port that matches with the given dst ip address isn't present {}");
        return;
    }
    flowRuleService.setRule(appId, instancePort.get().deviceId(), sBuilder.build(), treatment, PRIORITY_FORCED_ACL_RULE, DHCP_TABLE, false);
}
Also used : TpPort(org.onlab.packet.TpPort) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) PRIORITY_FORCED_ACL_RULE(org.onosproject.openstacknetworking.api.Constants.PRIORITY_FORCED_ACL_RULE) OpenstackFlowRuleService(org.onosproject.openstacknetworking.api.OpenstackFlowRuleService) DHCP_TABLE(org.onosproject.openstacknetworking.api.Constants.DHCP_TABLE) CoreService(org.onosproject.core.CoreService) InstancePort(org.onosproject.openstacknetworking.api.InstancePort) Argument(org.apache.karaf.shell.api.action.Argument) AbstractShellCommand.get(org.onosproject.cli.AbstractShellCommand.get) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment) Command(org.apache.karaf.shell.api.action.Command) Ethernet(org.onlab.packet.Ethernet) AbstractShellCommand(org.onosproject.cli.AbstractShellCommand) IPv4(org.onlab.packet.IPv4) TrafficSelector(org.onosproject.net.flow.TrafficSelector) OPENSTACK_NETWORKING_APP_ID(org.onosproject.openstacknetworking.api.Constants.OPENSTACK_NETWORKING_APP_ID) Service(org.apache.karaf.shell.api.action.lifecycle.Service) ApplicationId(org.onosproject.core.ApplicationId) Optional(java.util.Optional) DefaultTrafficSelector(org.onosproject.net.flow.DefaultTrafficSelector) IpAddress(org.onlab.packet.IpAddress) InstancePortService(org.onosproject.openstacknetworking.api.InstancePortService) InstancePortService(org.onosproject.openstacknetworking.api.InstancePortService) OpenstackFlowRuleService(org.onosproject.openstacknetworking.api.OpenstackFlowRuleService) InstancePort(org.onosproject.openstacknetworking.api.InstancePort) CoreService(org.onosproject.core.CoreService) TrafficSelector(org.onosproject.net.flow.TrafficSelector) DefaultTrafficSelector(org.onosproject.net.flow.DefaultTrafficSelector) IpAddress(org.onlab.packet.IpAddress) ApplicationId(org.onosproject.core.ApplicationId) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment)

Example 28 with IPv4

use of org.onlab.packet.IPv4 in project onos by opennetworkinglab.

the class OpenstackRoutingSnatIcmpHandlerTest method constructIcmpRequestPacket.

private Ethernet constructIcmpRequestPacket(IpAddress srcIp, MacAddress srcMac, IpAddress dstIp, MacAddress dstMac, byte icmpType) {
    try {
        ICMPEcho icmpEcho = new ICMPEcho();
        icmpEcho.setIdentifier((short) 0).setSequenceNum((short) 0);
        ByteBuffer byteBufferIcmpEcho = ByteBuffer.wrap(icmpEcho.serialize());
        ICMP icmp = new ICMP();
        icmp.setIcmpType(icmpType).setIcmpCode(icmpType == TYPE_ECHO_REQUEST ? CODE_ECHO_REQUEST : CODE_ECHO_REPLY).setChecksum((short) 0);
        icmp.setPayload(ICMPEcho.deserializer().deserialize(byteBufferIcmpEcho.array(), 0, ICMPEcho.ICMP_ECHO_HEADER_LENGTH));
        ByteBuffer byteBufferIcmp = ByteBuffer.wrap(icmp.serialize());
        IPv4 iPacket = new IPv4();
        iPacket.setDestinationAddress(dstIp.toString());
        iPacket.setSourceAddress(srcIp.toString());
        iPacket.setTtl((byte) 64);
        iPacket.setChecksum((short) 0);
        iPacket.setDiffServ((byte) 0);
        iPacket.setProtocol(IPv4.PROTOCOL_ICMP);
        iPacket.setPayload(ICMP.deserializer().deserialize(byteBufferIcmp.array(), 0, 8));
        Ethernet ethPacket = new Ethernet();
        ethPacket.setEtherType(TYPE_IPV4);
        ethPacket.setSourceMACAddress(srcMac);
        ethPacket.setDestinationMACAddress(dstMac);
        ethPacket.setPayload(iPacket);
        return ethPacket;
    } catch (DeserializationException e) {
        return null;
    }
}
Also used : ICMPEcho(org.onlab.packet.ICMPEcho) IPv4(org.onlab.packet.IPv4) Ethernet(org.onlab.packet.Ethernet) ByteBuffer(java.nio.ByteBuffer) DeserializationException(org.onlab.packet.DeserializationException) ICMP(org.onlab.packet.ICMP)

Example 29 with IPv4

use of org.onlab.packet.IPv4 in project onos by opennetworkinglab.

the class OpenstackSwitchingDhcpHandlerTest method constructDhcpPacket.

/**
 * Constructs an Ethernet packet containing a DHCP payload.
 *
 * @param msgType DHCP message type
 * @return Ethernet packet
 */
private Ethernet constructDhcpPacket(DHCP.MsgType msgType) {
    // Ethernet frame
    Ethernet ethFrame = new Ethernet();
    ethFrame.setSourceMACAddress(CLIENT_HOST.mac());
    ethFrame.setDestinationMACAddress(MacAddress.BROADCAST);
    ethFrame.setEtherType(Ethernet.TYPE_IPV4);
    ethFrame.setVlanID((short) 2);
    // IP packet
    IPv4 ipv4Pkt = new IPv4();
    ipv4Pkt.setSourceAddress(0);
    ipv4Pkt.setDestinationAddress(BROADCAST.toInt());
    ipv4Pkt.setTtl((byte) 127);
    // UDP datagram
    UDP udpDatagram = new UDP();
    udpDatagram.setSourcePort((byte) UDP.DHCP_CLIENT_PORT);
    udpDatagram.setDestinationPort((byte) UDP.DHCP_SERVER_PORT);
    // DHCP payload
    DHCP dhcp = new DHCP();
    dhcp.setOpCode(DHCP.OPCODE_REQUEST);
    dhcp.setYourIPAddress(0);
    dhcp.setServerIPAddress(0);
    dhcp.setTransactionId(TRANSACTION_ID);
    dhcp.setClientHardwareAddress(CLIENT_HOST.mac().toBytes());
    dhcp.setHardwareType(DHCP.HWTYPE_ETHERNET);
    dhcp.setHardwareAddressLength((byte) 6);
    // DHCP options start...
    DhcpOption option = new DhcpOption();
    List<DhcpOption> optionList = new ArrayList<>();
    // DHCP message type
    option.setCode(OptionCode_MessageType.getValue());
    option.setLength((byte) 1);
    byte[] optionData = { (byte) msgType.getValue() };
    option.setData(optionData);
    optionList.add(option);
    // DHCP requested IP address
    option = new DhcpOption();
    option.setCode(OptionCode_RequestedIP.getValue());
    option.setLength((byte) 4);
    optionData = Ip4Address.valueOf(EXPECTED_IP).toOctets();
    option.setData(optionData);
    optionList.add(option);
    // DHCP domain server
    Subnet subnet = dhcpHandler.osNetworkService.subnet("subnet");
    option = new DhcpOption();
    option.setCode(OptionCode_DomainServer.getValue());
    option.setLength((byte) 8);
    ByteBuffer dnsByteBuf = ByteBuffer.allocate(8);
    dnsByteBuf.put(DEFAULT_PRIMARY_DNS.toOctets());
    dnsByteBuf.put(DEFAULT_SECONDARY_DNS.toOctets());
    option.setData(dnsByteBuf.array());
    optionList.add(option);
    // MTU
    option = new DhcpOption();
    option.setCode(DHCP_OPTION_MTU);
    option.setLength((byte) 2);
    option.setData(ByteBuffer.allocate(2).putShort(MTU).array());
    optionList.add(option);
    // classless static route
    option = new DhcpOption();
    option.setCode(OptionCode_Classless_Static_Route.getValue());
    option.setLength((byte) HOST_ROUTES_SIZE);
    ByteBuffer hostRouteByteBuf = ByteBuffer.allocate(HOST_ROUTES_SIZE);
    subnet.getHostRoutes().forEach(h -> {
        IpPrefix ipPrefix = IpPrefix.valueOf(h.getDestination());
        hostRouteByteBuf.put(bytesDestinationDescriptor(ipPrefix));
        hostRouteByteBuf.put(Ip4Address.valueOf(h.getNexthop()).toOctets());
    });
    option.setData(hostRouteByteBuf.array());
    optionList.add(option);
    // default router address setup
    option = new DhcpOption();
    option.setCode(OptionCode_RouterAddress.getValue());
    option.setLength((byte) 4);
    option.setData(Ip4Address.valueOf(subnet.getGateway()).toOctets());
    optionList.add(option);
    // DHCP options end...
    option = new DhcpOption();
    option.setCode(OptionCode_END.getValue());
    option.setLength((byte) 1);
    optionList.add(option);
    dhcp.setOptions(optionList);
    udpDatagram.setPayload(dhcp);
    ipv4Pkt.setPayload(udpDatagram);
    ethFrame.setPayload(ipv4Pkt);
    return ethFrame;
}
Also used : UDP(org.onlab.packet.UDP) IpPrefix(org.onlab.packet.IpPrefix) Ethernet(org.onlab.packet.Ethernet) IPv4(org.onlab.packet.IPv4) ArrayList(java.util.ArrayList) DhcpOption(org.onlab.packet.dhcp.DhcpOption) Subnet(org.openstack4j.model.network.Subnet) ByteBuffer(java.nio.ByteBuffer) DHCP(org.onlab.packet.DHCP)

Example 30 with IPv4

use of org.onlab.packet.IPv4 in project onos by opennetworkinglab.

the class OpenstackTroubleshootManager method constructIcmpPacket.

/**
 * Constructs an ICMP packet with given source and destination IP/MAC.
 *
 * @param srcIp     source IP address
 * @param dstIp     destination IP address
 * @param srcMac    source MAC address
 * @param dstMac    destination MAC address
 * @param icmpId    ICMP identifier
 * @param icmpSeq   ICMP sequence number
 * @return an ethernet frame which contains ICMP payload
 */
private Ethernet constructIcmpPacket(IpAddress srcIp, IpAddress dstIp, MacAddress srcMac, MacAddress dstMac, short icmpId, short icmpSeq) {
    // Ethernet frame
    Ethernet ethFrame = new Ethernet();
    ethFrame.setEtherType(TYPE_IPV4);
    ethFrame.setSourceMACAddress(srcMac);
    ethFrame.setDestinationMACAddress(dstMac);
    // IP packet
    IPv4 iPacket = new IPv4();
    iPacket.setDestinationAddress(dstIp.toString());
    iPacket.setSourceAddress(srcIp.toString());
    iPacket.setTtl(TTL);
    iPacket.setProtocol(IPv4.PROTOCOL_ICMP);
    // ICMP packet
    ICMP icmp = new ICMP();
    icmp.setIcmpType(TYPE_ECHO_REQUEST).setIcmpCode(TYPE_ECHO_REQUEST).resetChecksum();
    // ICMP ECHO packet
    ICMPEcho icmpEcho = new ICMPEcho();
    icmpEcho.setIdentifier(icmpId).setSequenceNum(icmpSeq);
    ByteBuffer byteBufferIcmpEcho = ByteBuffer.wrap(icmpEcho.serialize());
    try {
        icmp.setPayload(ICMPEcho.deserializer().deserialize(byteBufferIcmpEcho.array(), 0, ICMPEcho.ICMP_ECHO_HEADER_LENGTH));
    } catch (DeserializationException e) {
        log.warn("Failed to deserialize ICMP ECHO REQUEST packet");
    }
    ByteBuffer byteBufferIcmp = ByteBuffer.wrap(icmp.serialize());
    try {
        iPacket.setPayload(ICMP.deserializer().deserialize(byteBufferIcmp.array(), 0, byteBufferIcmp.array().length));
    } catch (DeserializationException e) {
        log.warn("Failed to deserialize ICMP packet");
    }
    ethFrame.setPayload(iPacket);
    return ethFrame;
}
Also used : ICMPEcho(org.onlab.packet.ICMPEcho) Ethernet(org.onlab.packet.Ethernet) IPv4(org.onlab.packet.IPv4) ByteBuffer(java.nio.ByteBuffer) DeserializationException(org.onlab.packet.DeserializationException) ICMP(org.onlab.packet.ICMP)

Aggregations

IPv4 (org.onlab.packet.IPv4)54 Ethernet (org.onlab.packet.Ethernet)43 UDP (org.onlab.packet.UDP)24 DHCP (org.onlab.packet.DHCP)18 ICMP (org.onlab.packet.ICMP)17 IpAddress (org.onlab.packet.IpAddress)15 DefaultTrafficTreatment (org.onosproject.net.flow.DefaultTrafficTreatment)14 TrafficTreatment (org.onosproject.net.flow.TrafficTreatment)13 Test (org.junit.Test)12 Ip4Address (org.onlab.packet.Ip4Address)10 MacAddress (org.onlab.packet.MacAddress)10 ConnectPoint (org.onosproject.net.ConnectPoint)10 DefaultTrafficSelector (org.onosproject.net.flow.DefaultTrafficSelector)10 TrafficSelector (org.onosproject.net.flow.TrafficSelector)10 ByteBuffer (java.nio.ByteBuffer)8 Optional (java.util.Optional)8 Interface (org.onosproject.net.intf.Interface)8 DefaultOutboundPacket (org.onosproject.net.packet.DefaultOutboundPacket)8 DhcpOption (org.onlab.packet.dhcp.DhcpOption)7 ApplicationId (org.onosproject.core.ApplicationId)7