Search in sources :

Example 26 with IPv6

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

the class SimpleFabricRouting method checkVirtualGatewayIpPacket.

/**
 * handle Packet with dstIp=virtualGatewayIpAddresses.
 * returns true(handled) or false(not for virtual gateway)
 */
private boolean checkVirtualGatewayIpPacket(InboundPacket pkt, IpAddress srcIp, IpAddress dstIp) {
    // assume valid
    Ethernet ethPkt = pkt.parsed();
    MacAddress mac = simpleFabric.vMacForIp(dstIp);
    if (mac == null || !simpleFabric.isVirtualGatewayMac(ethPkt.getDestinationMAC())) {
        /* Destination MAC should be any of virtual gateway macs */
        return false;
    } else if (dstIp.isIp4()) {
        IPv4 ipv4Packet = (IPv4) ethPkt.getPayload();
        if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
            ICMP icmpPacket = (ICMP) ipv4Packet.getPayload();
            if (icmpPacket.getIcmpType() == ICMP.TYPE_ECHO_REQUEST) {
                log.info("IPV4 ICMP ECHO request to virtual gateway: " + "srcIp={} dstIp={} proto={}", srcIp, dstIp, ipv4Packet.getProtocol());
                TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(pkt.receivedFrom().port()).build();
                OutboundPacket packet = new DefaultOutboundPacket(pkt.receivedFrom().deviceId(), treatment, ByteBuffer.wrap(icmpPacket.buildIcmpReply(pkt.parsed()).serialize()));
                packetService.emit(packet);
                return true;
            }
        }
        log.warn("IPV4 packet to virtual gateway dropped: " + "srcIp={} dstIp={} proto={}", srcIp, dstIp, ipv4Packet.getProtocol());
        return true;
    } else if (dstIp.isIp6()) {
        // TODO: not tested yet (2017-07-20)
        IPv6 ipv6Packet = (IPv6) ethPkt.getPayload();
        if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
            ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
            if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST) {
                log.info("IPV6 ICMP6 ECHO request to virtual gateway: srcIp={} dstIp={} nextHeader={}", srcIp, dstIp, ipv6Packet.getNextHeader());
                TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(pkt.receivedFrom().port()).build();
                OutboundPacket packet = new DefaultOutboundPacket(pkt.receivedFrom().deviceId(), treatment, ByteBuffer.wrap(icmp6Packet.buildIcmp6Reply(pkt.parsed()).serialize()));
                packetService.emit(packet);
                return true;
            }
        }
        log.warn("IPV6 packet to virtual gateway dropped: srcIp={} dstIp={} nextHeader={}", srcIp, dstIp, ipv6Packet.getNextHeader());
        return true;
    }
    // unknown traffic
    return false;
}
Also used : IPv6(org.onlab.packet.IPv6) Ethernet(org.onlab.packet.Ethernet) IPv4(org.onlab.packet.IPv4) DefaultOutboundPacket(org.onosproject.net.packet.DefaultOutboundPacket) MacAddress(org.onlab.packet.MacAddress) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) ICMP6(org.onlab.packet.ICMP6) OutboundPacket(org.onosproject.net.packet.OutboundPacket) DefaultOutboundPacket(org.onosproject.net.packet.DefaultOutboundPacket) ICMP(org.onlab.packet.ICMP)

Example 27 with IPv6

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

the class HostMonitorTest method testMonitorIpv6HostDoesNotExistWithVlan.

@Test
public void testMonitorIpv6HostDoesNotExistWithVlan() throws Exception {
    HostManager hostManager = createMock(HostManager.class);
    DeviceId devId = DeviceId.deviceId("fake");
    short vlan = 5;
    Device device = createMock(Device.class);
    expect(device.id()).andReturn(devId).anyTimes();
    replay(device);
    PortNumber portNum = PortNumber.portNumber(1L);
    Port port = createMock(Port.class);
    expect(port.number()).andReturn(portNum).anyTimes();
    expect(port.isEnabled()).andReturn(true).anyTimes();
    replay(port);
    TestDeviceService deviceService = new TestDeviceService();
    deviceService.addDevice(device, Collections.singleton(port));
    ConnectPoint cp = new ConnectPoint(devId, portNum);
    expect(hostManager.getHostsByIp(TARGET_IPV6_ADDR)).andReturn(Collections.emptySet()).anyTimes();
    replay(hostManager);
    InterfaceService interfaceService = createMock(InterfaceService.class);
    expect(interfaceService.getMatchingInterfaces(TARGET_IPV6_ADDR)).andReturn(Collections.singleton(new Interface(Interface.NO_INTERFACE_NAME, cp, Collections.singletonList(IA2), sourceMac2, VlanId.vlanId(vlan)))).anyTimes();
    replay(interfaceService);
    TestPacketService packetService = new TestPacketService();
    // Run the test
    hostMonitor = new HostMonitor(packetService, hostManager, interfaceService, edgePortService, deviceService);
    hostMonitor.addMonitoringFor(TARGET_IPV6_ADDR);
    hostMonitor.run();
    // Check that a packet was sent to our PacketService and that it has
    // the properties we expect
    assertEquals(2, packetService.packets.size());
    OutboundPacket packet = packetService.packets.get(0);
    // Check the output port is correct
    assertEquals(1, packet.treatment().immediate().size());
    Instruction instruction = packet.treatment().immediate().get(0);
    assertTrue(instruction instanceof OutputInstruction);
    OutputInstruction oi = (OutputInstruction) instruction;
    assertEquals(portNum, oi.port());
    // Check the output packet is correct (well the important bits anyway)
    final byte[] pktData = new byte[packet.data().remaining()];
    packet.data().get(pktData);
    Ethernet eth = Ethernet.deserializer().deserialize(pktData, 0, pktData.length);
    assertEquals(vlan, eth.getVlanID());
    IPv6 ipv6 = (IPv6) eth.getPayload();
    assertArrayEquals(SOURCE_IPV6_ADDR.toOctets(), ipv6.getSourceAddress());
    NeighborSolicitation ns = (NeighborSolicitation) ipv6.getPayload().getPayload();
    assertArrayEquals(sourceMac2.toBytes(), ns.getOptions().get(0).data());
    assertArrayEquals(TARGET_IPV6_ADDR.toOctets(), ns.getTargetAddress());
}
Also used : OutputInstruction(org.onosproject.net.flow.instructions.Instructions.OutputInstruction) IPv6(org.onlab.packet.IPv6) DeviceId(org.onosproject.net.DeviceId) Device(org.onosproject.net.Device) Port(org.onosproject.net.Port) OutputInstruction(org.onosproject.net.flow.instructions.Instructions.OutputInstruction) Instruction(org.onosproject.net.flow.instructions.Instruction) ConnectPoint(org.onosproject.net.ConnectPoint) OutboundPacket(org.onosproject.net.packet.OutboundPacket) InterfaceService(org.onosproject.net.intf.InterfaceService) Ethernet(org.onlab.packet.Ethernet) NeighborSolicitation(org.onlab.packet.ndp.NeighborSolicitation) PortNumber(org.onosproject.net.PortNumber) Interface(org.onosproject.net.intf.Interface) Test(org.junit.Test)

Example 28 with IPv6

use of org.onlab.packet.IPv6 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)

Example 29 with IPv6

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

the class NeighborSolicitationTest method testBuildNdpSolicit.

/**
 * Tests regular non-DAD neighbor solicitation.
 */
@Test
public void testBuildNdpSolicit() throws Exception {
    final Ethernet ethPacket = NeighborSolicitation.buildNdpSolicit(TARGET_IP, SRC_IP, DST_IP, SRC_MAC, DST_MAC, VLAN_ID);
    assertTrue(ethPacket.getDestinationMAC().equals(DST_MAC));
    assertTrue(ethPacket.getSourceMAC().equals(SRC_MAC));
    assertTrue(ethPacket.getEtherType() == Ethernet.TYPE_IPV6);
    assertTrue(ethPacket.getVlanID() == VLAN_ID.id());
    final IPv6 ipPacket = (IPv6) ethPacket.getPayload();
    assertArrayEquals(ipPacket.getSourceAddress(), SRC_IP.toOctets());
    assertArrayEquals(ipPacket.getDestinationAddress(), DST_IP.toOctets());
    final ICMP6 icmp6Packet = (ICMP6) ipPacket.getPayload();
    final NeighborSolicitation nsPacket = (NeighborSolicitation) icmp6Packet.getPayload();
    assertArrayEquals(nsPacket.getTargetAddress(), TARGET_IP.toOctets());
    assertEquals("Non-DAD NS should have 1 option", 1, nsPacket.getOptions().size());
    assertEquals("The option should be SRC_LL_ADDR type", TYPE_SOURCE_LL_ADDRESS, nsPacket.getOptions().stream().findFirst().get().type());
}
Also used : IPv6(org.onlab.packet.IPv6) Ethernet(org.onlab.packet.Ethernet) ICMP6(org.onlab.packet.ICMP6) Test(org.junit.Test)

Example 30 with IPv6

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

the class NeighborSolicitationTest method testBuildNdpSolicitDad.

/**
 * Tests DAD neighbor solicitation.
 * Source IP should be all-zero.
 */
@Test
public void testBuildNdpSolicitDad() throws Exception {
    Ethernet ethPacket = NeighborSolicitation.buildNdpSolicit(TARGET_IP, Ip6Address.ZERO, DST_IP, SRC_MAC, DST_MAC, VLAN_ID);
    IPv6 ipPacket = (IPv6) ethPacket.getPayload();
    ICMP6 icmp6Packet = (ICMP6) ipPacket.getPayload();
    NeighborSolicitation nsPacket = (NeighborSolicitation) icmp6Packet.getPayload();
    assertEquals("DAD NS should have no option", 0, nsPacket.getOptions().size());
}
Also used : IPv6(org.onlab.packet.IPv6) Ethernet(org.onlab.packet.Ethernet) ICMP6(org.onlab.packet.ICMP6) Test(org.junit.Test)

Aggregations

IPv6 (org.onlab.packet.IPv6)34 Ethernet (org.onlab.packet.Ethernet)28 UDP (org.onlab.packet.UDP)18 Test (org.junit.Test)17 DHCP6 (org.onlab.packet.DHCP6)17 ConnectPoint (org.onosproject.net.ConnectPoint)10 ICMP6 (org.onlab.packet.ICMP6)9 Interface (org.onosproject.net.intf.Interface)9 MacAddress (org.onlab.packet.MacAddress)8 OutboundPacket (org.onosproject.net.packet.OutboundPacket)8 Ip6Address (org.onlab.packet.Ip6Address)7 DeviceId (org.onosproject.net.DeviceId)7 DefaultTrafficTreatment (org.onosproject.net.flow.DefaultTrafficTreatment)7 InterfaceService (org.onosproject.net.intf.InterfaceService)7 ArrayList (java.util.ArrayList)6 VlanId (org.onlab.packet.VlanId)6 PacketContext (org.onosproject.net.packet.PacketContext)6 Logger (org.slf4j.Logger)6 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)5 ByteBuffer (java.nio.ByteBuffer)5