Search in sources :

Example 1 with PROTOCOL_ICMP6

use of org.onlab.packet.IPv6.PROTOCOL_ICMP6 in project onos by opennetworkinglab.

the class ReactiveForwarding method installRule.

// Install a rule forwarding the packet to the specified port.
private void installRule(PacketContext context, PortNumber portNumber, ReactiveForwardMetrics macMetrics) {
    // 
    // We don't support (yet) buffer IDs in the Flow Service so
    // packet out first.
    // 
    Ethernet inPkt = context.inPacket().parsed();
    TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
    // If PacketOutOnly or ARP packet than forward directly to output port
    if (packetOutOnly || inPkt.getEtherType() == Ethernet.TYPE_ARP) {
        packetOut(context, portNumber, macMetrics);
        return;
    }
    // 
    if (matchDstMacOnly) {
        selectorBuilder.matchEthDst(inPkt.getDestinationMAC());
    } else {
        selectorBuilder.matchInPort(context.inPacket().receivedFrom().port()).matchEthSrc(inPkt.getSourceMAC()).matchEthDst(inPkt.getDestinationMAC());
        // If configured Match Vlan ID
        if (matchVlanId && inPkt.getVlanID() != Ethernet.VLAN_UNTAGGED) {
            selectorBuilder.matchVlanId(VlanId.vlanId(inPkt.getVlanID()));
        }
        // 
        if (matchIpv4Address && inPkt.getEtherType() == Ethernet.TYPE_IPV4) {
            IPv4 ipv4Packet = (IPv4) inPkt.getPayload();
            byte ipv4Protocol = ipv4Packet.getProtocol();
            Ip4Prefix matchIp4SrcPrefix = Ip4Prefix.valueOf(ipv4Packet.getSourceAddress(), Ip4Prefix.MAX_MASK_LENGTH);
            Ip4Prefix matchIp4DstPrefix = Ip4Prefix.valueOf(ipv4Packet.getDestinationAddress(), Ip4Prefix.MAX_MASK_LENGTH);
            selectorBuilder.matchEthType(Ethernet.TYPE_IPV4).matchIPSrc(matchIp4SrcPrefix).matchIPDst(matchIp4DstPrefix);
            if (matchIpv4Dscp) {
                byte dscp = ipv4Packet.getDscp();
                byte ecn = ipv4Packet.getEcn();
                selectorBuilder.matchIPDscp(dscp).matchIPEcn(ecn);
            }
            if (matchTcpUdpPorts && ipv4Protocol == IPv4.PROTOCOL_TCP) {
                TCP tcpPacket = (TCP) ipv4Packet.getPayload();
                selectorBuilder.matchIPProtocol(ipv4Protocol).matchTcpSrc(TpPort.tpPort(tcpPacket.getSourcePort())).matchTcpDst(TpPort.tpPort(tcpPacket.getDestinationPort()));
            }
            if (matchTcpUdpPorts && ipv4Protocol == IPv4.PROTOCOL_UDP) {
                UDP udpPacket = (UDP) ipv4Packet.getPayload();
                selectorBuilder.matchIPProtocol(ipv4Protocol).matchUdpSrc(TpPort.tpPort(udpPacket.getSourcePort())).matchUdpDst(TpPort.tpPort(udpPacket.getDestinationPort()));
            }
            if (matchIcmpFields && ipv4Protocol == IPv4.PROTOCOL_ICMP) {
                ICMP icmpPacket = (ICMP) ipv4Packet.getPayload();
                selectorBuilder.matchIPProtocol(ipv4Protocol).matchIcmpType(icmpPacket.getIcmpType()).matchIcmpCode(icmpPacket.getIcmpCode());
            }
        }
        // 
        if (matchIpv6Address && inPkt.getEtherType() == Ethernet.TYPE_IPV6) {
            IPv6 ipv6Packet = (IPv6) inPkt.getPayload();
            byte ipv6NextHeader = ipv6Packet.getNextHeader();
            Ip6Prefix matchIp6SrcPrefix = Ip6Prefix.valueOf(ipv6Packet.getSourceAddress(), Ip6Prefix.MAX_MASK_LENGTH);
            Ip6Prefix matchIp6DstPrefix = Ip6Prefix.valueOf(ipv6Packet.getDestinationAddress(), Ip6Prefix.MAX_MASK_LENGTH);
            selectorBuilder.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Src(matchIp6SrcPrefix).matchIPv6Dst(matchIp6DstPrefix);
            if (matchIpv6FlowLabel) {
                selectorBuilder.matchIPv6FlowLabel(ipv6Packet.getFlowLabel());
            }
            if (matchTcpUdpPorts && ipv6NextHeader == IPv6.PROTOCOL_TCP) {
                TCP tcpPacket = (TCP) ipv6Packet.getPayload();
                selectorBuilder.matchIPProtocol(ipv6NextHeader).matchTcpSrc(TpPort.tpPort(tcpPacket.getSourcePort())).matchTcpDst(TpPort.tpPort(tcpPacket.getDestinationPort()));
            }
            if (matchTcpUdpPorts && ipv6NextHeader == IPv6.PROTOCOL_UDP) {
                UDP udpPacket = (UDP) ipv6Packet.getPayload();
                selectorBuilder.matchIPProtocol(ipv6NextHeader).matchUdpSrc(TpPort.tpPort(udpPacket.getSourcePort())).matchUdpDst(TpPort.tpPort(udpPacket.getDestinationPort()));
            }
            if (matchIcmpFields && ipv6NextHeader == IPv6.PROTOCOL_ICMP6) {
                ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
                selectorBuilder.matchIPProtocol(ipv6NextHeader).matchIcmpv6Type(icmp6Packet.getIcmpType()).matchIcmpv6Code(icmp6Packet.getIcmpCode());
            }
        }
    }
    TrafficTreatment treatment;
    if (inheritFlowTreatment) {
        treatment = context.treatmentBuilder().setOutput(portNumber).build();
    } else {
        treatment = DefaultTrafficTreatment.builder().setOutput(portNumber).build();
    }
    ForwardingObjective forwardingObjective = DefaultForwardingObjective.builder().withSelector(selectorBuilder.build()).withTreatment(treatment).withPriority(flowPriority).withFlag(ForwardingObjective.Flag.VERSATILE).fromApp(appId).makeTemporary(flowTimeout).add();
    flowObjectiveService.forward(context.inPacket().receivedFrom().deviceId(), forwardingObjective);
    forwardPacket(macMetrics);
    // 
    if (packetOutOfppTable) {
        packetOut(context, PortNumber.TABLE, macMetrics);
    } else {
        packetOut(context, portNumber, macMetrics);
    }
}
Also used : TCP(org.onlab.packet.TCP) UDP(org.onlab.packet.UDP) IPv6(org.onlab.packet.IPv6) IPv4(org.onlab.packet.IPv4) ForwardingObjective(org.onosproject.net.flowobjective.ForwardingObjective) DefaultForwardingObjective(org.onosproject.net.flowobjective.DefaultForwardingObjective) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) Ip6Prefix(org.onlab.packet.Ip6Prefix) Ethernet(org.onlab.packet.Ethernet) TrafficSelector(org.onosproject.net.flow.TrafficSelector) DefaultTrafficSelector(org.onosproject.net.flow.DefaultTrafficSelector) Ip4Prefix(org.onlab.packet.Ip4Prefix) ICMP6(org.onlab.packet.ICMP6) ICMP(org.onlab.packet.ICMP)

Example 2 with PROTOCOL_ICMP6

use of org.onlab.packet.IPv6.PROTOCOL_ICMP6 in project onos by opennetworkinglab.

the class DefaultNeighbourMessageContext method createNdpContext.

/**
 * Extracts context information from NDP packets.
 *
 * @param eth input Ethernet frame that is thought to be NDP
 * @param inPort in port
 * @param actions actions to take
 * @return MessageContext object if the packet was a valid NDP packet,
 * otherwise null
 */
private static NeighbourMessageContext createNdpContext(Ethernet eth, ConnectPoint inPort, NeighbourMessageActions actions) {
    if (eth.getEtherType() != Ethernet.TYPE_IPV6) {
        return null;
    }
    IPv6 ipv6 = (IPv6) eth.getPayload();
    if (ipv6.getNextHeader() != IPv6.PROTOCOL_ICMP6) {
        return null;
    }
    ICMP6 icmpv6 = (ICMP6) ipv6.getPayload();
    IpAddress sender = Ip6Address.valueOf(ipv6.getSourceAddress());
    IpAddress target;
    NeighbourMessageType type;
    if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_SOLICITATION) {
        type = NeighbourMessageType.REQUEST;
        NeighborSolicitation nsol = (NeighborSolicitation) icmpv6.getPayload();
        target = Ip6Address.valueOf(nsol.getTargetAddress());
    } else if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_ADVERTISEMENT) {
        type = NeighbourMessageType.REPLY;
        /*
             * sender and target are the same in the reply.
             * We use as target the destination ip.
             */
        target = Ip6Address.valueOf(ipv6.getDestinationAddress());
    } else {
        return null;
    }
    return new DefaultNeighbourMessageContext(actions, eth, inPort, NeighbourProtocol.NDP, type, target, sender);
}
Also used : IPv6(org.onlab.packet.IPv6) NeighborSolicitation(org.onlab.packet.ndp.NeighborSolicitation) IpAddress(org.onlab.packet.IpAddress) NeighbourMessageType(org.onosproject.net.neighbour.NeighbourMessageType) ICMP6(org.onlab.packet.ICMP6)

Example 3 with PROTOCOL_ICMP6

use of org.onlab.packet.IPv6.PROTOCOL_ICMP6 in project onos by opennetworkinglab.

the class NeighborAdvertisement method buildNdpAdv.

/**
 * Builds an NDP reply based on a request.
 *
 * @param srcIp   the IP address to use as the reply source
 * @param srcMac  the MAC address to use as the reply source
 * @param request the Neighbor Solicitation request we got
 * @return an Ethernet frame containing the Neighbor Advertisement reply
 */
public static Ethernet buildNdpAdv(Ip6Address srcIp, MacAddress srcMac, Ethernet request) {
    checkNotNull(srcIp, "IP address cannot be null");
    checkNotNull(srcMac, "MAC address cannot be null");
    checkNotNull(request, "Request cannot be null");
    checkArgument(request.getEtherType() == Ethernet.TYPE_IPV6, "EtherType must be IPv6");
    final IPv6 ipv6Request = (IPv6) request.getPayload();
    checkArgument(ipv6Request.getNextHeader() == IPv6.PROTOCOL_ICMP6, "Protocol must be ICMP6");
    final ICMP6 icmpv6 = (ICMP6) ipv6Request.getPayload();
    checkArgument(icmpv6.getIcmpType() == ICMP6.NEIGHBOR_SOLICITATION, "ICMP6 type must be NEIGHBOR_SOLICITATION");
    Ethernet eth = new Ethernet();
    eth.setDestinationMACAddress(request.getSourceMAC());
    eth.setSourceMACAddress(srcMac);
    eth.setEtherType(Ethernet.TYPE_IPV6);
    eth.setVlanID(request.getVlanID());
    IPv6 ipv6 = new IPv6();
    ipv6.setSourceAddress(srcIp.toOctets());
    ipv6.setDestinationAddress(ipv6Request.getSourceAddress());
    ipv6.setHopLimit(NDP_HOP_LIMIT);
    ipv6.setNextHeader(IPv6.PROTOCOL_ICMP6);
    ICMP6 icmp6 = new ICMP6();
    icmp6.setIcmpType(ICMP6.NEIGHBOR_ADVERTISEMENT);
    icmp6.setIcmpCode(RESERVED_CODE);
    NeighborAdvertisement nadv = new NeighborAdvertisement();
    nadv.setTargetAddress(srcIp.toOctets());
    nadv.setSolicitedFlag(NDP_SOLICITED_FLAG);
    nadv.setOverrideFlag(NDP_OVERRIDE_FLAG);
    nadv.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, srcMac.toBytes());
    icmp6.setPayload(nadv);
    ipv6.setPayload(icmp6);
    eth.setPayload(ipv6);
    return eth;
}
Also used : IPv6(org.onlab.packet.IPv6) Ethernet(org.onlab.packet.Ethernet) ICMP6(org.onlab.packet.ICMP6)

Example 4 with PROTOCOL_ICMP6

use of org.onlab.packet.IPv6.PROTOCOL_ICMP6 in project onos by opennetworkinglab.

the class NeighborAdvertisementTest method testBuildNdpAdv.

/**
 * Test Neighbor Advertisement reply build.
 */
@Test
public void testBuildNdpAdv() {
    Ethernet eth = new Ethernet();
    eth.setSourceMACAddress(MAC_ADDRESS);
    eth.setDestinationMACAddress(MAC_ADDRESS2);
    IPv6 ipv6 = new IPv6();
    ipv6.setSourceAddress(IPV6_SOURCE_ADDRESS);
    ipv6.setDestinationAddress(IPV6_DESTINATION_ADDRESS);
    ipv6.setNextHeader(IPv6.PROTOCOL_ICMP6);
    eth.setEtherType(Ethernet.TYPE_IPV6);
    eth.setPayload(ipv6);
    ICMP6 icmp6 = new ICMP6();
    icmp6.setIcmpType(ICMP6.NEIGHBOR_SOLICITATION);
    icmp6.setIcmpCode(NeighborAdvertisement.RESERVED_CODE);
    ipv6.setPayload(icmp6);
    final Ethernet ethResponse = NeighborAdvertisement.buildNdpAdv(IP_6_ADDRESS, MAC_ADDRESS2, eth);
    assertTrue(ethResponse.getDestinationMAC().equals(MAC_ADDRESS));
    assertTrue(ethResponse.getSourceMAC().equals(MAC_ADDRESS2));
    assertTrue(ethResponse.getEtherType() == Ethernet.TYPE_IPV6);
    final IPv6 responseIpv6 = (IPv6) ethResponse.getPayload();
    assertArrayEquals(responseIpv6.getSourceAddress(), ipv6.getDestinationAddress());
    assertArrayEquals(responseIpv6.getDestinationAddress(), ipv6.getSourceAddress());
    assertTrue(responseIpv6.getNextHeader() == IPv6.PROTOCOL_ICMP6);
    final ICMP6 responseIcmp6 = (ICMP6) responseIpv6.getPayload();
    assertTrue(responseIcmp6.getIcmpType() == ICMP6.NEIGHBOR_ADVERTISEMENT);
    assertTrue(responseIcmp6.getIcmpCode() == NeighborAdvertisement.RESERVED_CODE);
    final NeighborAdvertisement responseNadv = (NeighborAdvertisement) responseIcmp6.getPayload();
    assertArrayEquals(responseNadv.getTargetAddress(), IPV6_DESTINATION_ADDRESS);
    assertTrue(responseNadv.getSolicitedFlag() == NeighborAdvertisement.NDP_SOLICITED_FLAG);
    assertTrue(responseNadv.getOverrideFlag() == NeighborAdvertisement.NDP_OVERRIDE_FLAG);
    assertThat(responseNadv.getOptions(), hasItem(hasOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, MAC_ADDRESS2.toBytes())));
}
Also used : IPv6(org.onlab.packet.IPv6) Ethernet(org.onlab.packet.Ethernet) ICMP6(org.onlab.packet.ICMP6) Test(org.junit.Test)

Example 5 with PROTOCOL_ICMP6

use of org.onlab.packet.IPv6.PROTOCOL_ICMP6 in project onos by opennetworkinglab.

the class ICMP6 method serialize.

@Override
public byte[] serialize() {
    byte[] payloadData = null;
    if (this.payload != null) {
        this.payload.setParent(this);
        payloadData = this.payload.serialize();
    }
    int payloadLength = 0;
    if (payloadData != null) {
        payloadLength = payloadData.length;
    }
    final byte[] data = new byte[HEADER_LENGTH + payloadLength];
    final ByteBuffer bbData = ByteBuffer.wrap(data);
    // Creating ByteBuffer for checksum calculation
    final byte[] checksumData = new byte[IPv6.FIXED_HEADER_LENGTH + HEADER_LENGTH + payloadLength];
    final ByteBuffer bbChecksum = ByteBuffer.wrap(checksumData);
    // 
    // Creating IPv6 Pseudo Header for checksum calculation according
    // to RFC 4443 and RFC 2460
    // 
    IPv6 ipv6Parent = null;
    for (IPacket p = this.parent; p != null; p = p.getParent()) {
        if (p instanceof IPv6) {
            ipv6Parent = (IPv6) p;
            break;
        }
    }
    if (ipv6Parent != null) {
        bbChecksum.put(ipv6Parent.getSourceAddress());
        bbChecksum.put(ipv6Parent.getDestinationAddress());
    } else {
        // NOTE: IPv6 source and destination addresses unknown. Use zeroes.
        bbChecksum.put(ZERO_ADDRESS);
        bbChecksum.put(ZERO_ADDRESS);
    }
    bbChecksum.putInt(HEADER_LENGTH + payloadLength);
    bbChecksum.put((byte) 0);
    bbChecksum.put((byte) 0);
    bbChecksum.put((byte) 0);
    bbChecksum.put(IPv6.PROTOCOL_ICMP6);
    bbChecksum.put(this.icmpType);
    bbChecksum.put(this.icmpCode);
    bbChecksum.put((byte) 0);
    bbChecksum.put((byte) 0);
    bbData.put(this.icmpType);
    bbData.put(this.icmpCode);
    bbData.putShort(this.checksum);
    if (payloadData != null) {
        bbData.put(payloadData);
        bbChecksum.put(payloadData);
    }
    if (this.parent != null) {
        if (this.parent instanceof IPv6) {
            ((IPv6) this.parent).setNextHeader(IPv6.PROTOCOL_ICMP6);
        } else if (this.parent instanceof IExtensionHeader) {
            ((IExtensionHeader) this.parent).setNextHeader(IPv6.PROTOCOL_ICMP6);
        }
    }
    // compute checksum if needed
    if (this.checksum == 0) {
        bbData.rewind();
        bbChecksum.rewind();
        int accumulation = 0;
        for (int i = 0; i < checksumData.length / 2; ++i) {
            accumulation += 0xffff & bbChecksum.getShort();
        }
        // pad to an even number of shorts
        if (checksumData.length % 2 > 0) {
            accumulation += (bbChecksum.get() & 0xff) << 8;
        }
        accumulation = (accumulation >> 16 & 0xffff) + (accumulation & 0xffff);
        this.checksum = (short) (~accumulation & 0xffff);
        bbData.putShort(2, this.checksum);
    }
    return data;
}
Also used : IExtensionHeader(org.onlab.packet.ipv6.IExtensionHeader) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ICMP6 (org.onlab.packet.ICMP6)5 IPv6 (org.onlab.packet.IPv6)5 Ethernet (org.onlab.packet.Ethernet)4 ICMP (org.onlab.packet.ICMP)2 IPv4 (org.onlab.packet.IPv4)2 DefaultTrafficTreatment (org.onosproject.net.flow.DefaultTrafficTreatment)2 TrafficTreatment (org.onosproject.net.flow.TrafficTreatment)2 ByteBuffer (java.nio.ByteBuffer)1 Test (org.junit.Test)1 Ip4Prefix (org.onlab.packet.Ip4Prefix)1 Ip6Prefix (org.onlab.packet.Ip6Prefix)1 IpAddress (org.onlab.packet.IpAddress)1 MacAddress (org.onlab.packet.MacAddress)1 TCP (org.onlab.packet.TCP)1 UDP (org.onlab.packet.UDP)1 IExtensionHeader (org.onlab.packet.ipv6.IExtensionHeader)1 NeighborSolicitation (org.onlab.packet.ndp.NeighborSolicitation)1 DefaultTrafficSelector (org.onosproject.net.flow.DefaultTrafficSelector)1 TrafficSelector (org.onosproject.net.flow.TrafficSelector)1 DefaultForwardingObjective (org.onosproject.net.flowobjective.DefaultForwardingObjective)1