Search in sources :

Example 1 with NeighborSolicitation

use of org.onlab.packet.ndp.NeighborSolicitation 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 2 with NeighborSolicitation

use of org.onlab.packet.ndp.NeighborSolicitation in project onos by opennetworkinglab.

the class HostMonitorTest method testMonitorIpv6HostDoesNotExist.

@Test
public void testMonitorIpv6HostDoesNotExist() throws Exception {
    HostManager hostManager = createMock(HostManager.class);
    DeviceId devId = DeviceId.deviceId("fake");
    Device device = createMock(Device.class);
    expect(device.id()).andReturn(devId).anyTimes();
    replay(device);
    PortNumber portNum = PortNumber.portNumber(2L);
    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.NONE))).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(Ethernet.VLAN_UNTAGGED, 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 3 with NeighborSolicitation

use of org.onlab.packet.ndp.NeighborSolicitation in project onos by opennetworkinglab.

the class Ethernet method toString.

/*
     * (non-Javadoc)
     *
     * @see java.lang.Object#toString(java.lang.Object)
     */
@Override
public String toString() {
    final StringBuilder sb = new StringBuilder("\n");
    final IPacket pkt = this.getPayload();
    if (pkt instanceof ARP) {
        sb.append("arp");
    } else if (pkt instanceof MPLS) {
        sb.append("mpls");
    } else if (pkt instanceof LLDP) {
        sb.append("lldp");
    } else if (pkt instanceof ICMP) {
        sb.append("icmp");
    } else if (pkt instanceof IPv4) {
        sb.append("ip");
    } else if (pkt instanceof DHCP) {
        sb.append("dhcp");
    } else {
        // Just print the ethertype
        sb.append(String.format(HEX_PROTO, Integer.toHexString(this.getEtherType() & 0xffff)));
    }
    if (this.getQinQVID() != Ethernet.VLAN_UNTAGGED) {
        sb.append("\ndl_qinqVlan: ");
        sb.append(this.getQinQVID());
        sb.append("\ndl_qinqVlan_pcp: ");
        sb.append(this.getQinQPriorityCode());
    }
    sb.append("\ndl_vlan: ");
    if (this.getVlanID() == Ethernet.VLAN_UNTAGGED) {
        sb.append("untagged");
    } else {
        sb.append(this.getVlanID());
    }
    sb.append("\ndl_vlan_pcp: ");
    sb.append(this.getPriorityCode());
    sb.append("\ndl_src: ");
    sb.append(bytesToHex(this.getSourceMACAddress()));
    sb.append("\ndl_dst: ");
    sb.append(bytesToHex(this.getDestinationMACAddress()));
    if (pkt instanceof ARP) {
        final ARP p = (ARP) pkt;
        sb.append("\nnw_src: ");
        sb.append(IPv4.fromIPv4Address(IPv4.toIPv4Address(p.getSenderProtocolAddress())));
        sb.append("\nnw_dst: ");
        sb.append(IPv4.fromIPv4Address(IPv4.toIPv4Address(p.getTargetProtocolAddress())));
    } else if (pkt instanceof MPLS) {
        final MPLS p = (MPLS) pkt;
        sb.append("\nmpls: ");
        sb.append(this.etherType == MPLS_UNICAST ? "unicast" : "multicast");
        sb.append("\nmpls_label: ");
        sb.append(p.label);
    } else if (pkt instanceof LLDP) {
        sb.append("lldp packet");
    } else if (pkt instanceof ICMP) {
        final ICMP icmp = (ICMP) pkt;
        sb.append("\nicmp_type: ");
        sb.append(icmp.getIcmpType());
        sb.append("\nicmp_code: ");
        sb.append(icmp.getIcmpCode());
    } else if (pkt instanceof IPv4) {
        final IPv4 p = (IPv4) pkt;
        sb.append("\nnw_src: ");
        sb.append(IPv4.fromIPv4Address(p.getSourceAddress()));
        sb.append("\nnw_dst: ");
        sb.append(IPv4.fromIPv4Address(p.getDestinationAddress()));
        sb.append("\nnw_tos: ");
        sb.append(p.getDiffServ());
        sb.append("\nnw_proto: ");
        sb.append(p.getProtocol());
        IPacket payload = pkt.getPayload();
        if (payload != null) {
            if (payload instanceof TCP) {
                sb.append("\ntp_src: ");
                sb.append(((TCP) payload).getSourcePort());
                sb.append("\ntp_dst: ");
                sb.append(((TCP) payload).getDestinationPort());
            } else if (payload instanceof UDP) {
                sb.append("\ntp_src: ");
                sb.append(((UDP) payload).getSourcePort());
                sb.append("\ntp_dst: ");
                sb.append(((UDP) payload).getDestinationPort());
            } else if (payload instanceof ICMP) {
                final ICMP icmp = (ICMP) payload;
                sb.append("\nicmp_type: ");
                sb.append(icmp.getIcmpType());
                sb.append("\nicmp_code: ");
                sb.append(icmp.getIcmpCode());
            }
        }
    } else if (pkt instanceof IPv6) {
        final IPv6 ipv6 = (IPv6) pkt;
        sb.append("\nipv6_src: ");
        sb.append(Ip6Address.valueOf(ipv6.getSourceAddress()).toString());
        sb.append("\nipv6_dst: ");
        sb.append(Ip6Address.valueOf(ipv6.getDestinationAddress()).toString());
        sb.append("\nipv6_proto: ");
        sb.append(ipv6.getNextHeader());
        IPacket payload = pkt.getPayload();
        if (payload != null && payload instanceof ICMP6) {
            final ICMP6 icmp6 = (ICMP6) payload;
            sb.append("\nicmp6_type: ");
            sb.append(icmp6.getIcmpType());
            sb.append("\nicmp6_code: ");
            sb.append(icmp6.getIcmpCode());
            payload = payload.getPayload();
            if (payload != null) {
                if (payload instanceof NeighborSolicitation) {
                    final NeighborSolicitation ns = (NeighborSolicitation) payload;
                    sb.append("\nns_target_addr: ");
                    sb.append(Ip6Address.valueOf(ns.getTargetAddress()).toString());
                    ns.getOptions().forEach(option -> {
                        sb.append("\noption_type: ");
                        sb.append(option.type());
                        sb.append("\noption_data: ");
                        sb.append(bytesToHex(option.data()));
                    });
                } else if (payload instanceof NeighborAdvertisement) {
                    final NeighborAdvertisement na = (NeighborAdvertisement) payload;
                    sb.append("\nna_target_addr: ");
                    sb.append(Ip6Address.valueOf(na.getTargetAddress()).toString());
                    sb.append("\nna_solicited_flag: ");
                    sb.append(na.getSolicitedFlag());
                    sb.append("\nna_router_flag: ");
                    sb.append(na.getRouterFlag());
                    sb.append("\nna_override_flag: ");
                    sb.append(na.getOverrideFlag());
                    na.getOptions().forEach(option -> {
                        sb.append("\noption_type: ");
                        sb.append(option.type());
                        sb.append("\noption_data: ");
                        sb.append(bytesToHex(option.data()));
                    });
                } else if (payload instanceof RouterSolicitation) {
                    final RouterSolicitation rs = (RouterSolicitation) payload;
                    sb.append("\nrs");
                    rs.getOptions().forEach(option -> {
                        sb.append("\noption_type: ");
                        sb.append(option.type());
                        sb.append("\noption_data: ");
                        sb.append(bytesToHex(option.data()));
                    });
                } else if (payload instanceof RouterAdvertisement) {
                    final RouterAdvertisement ra = (RouterAdvertisement) payload;
                    sb.append("\nra_hop_limit: ");
                    sb.append(ra.getCurrentHopLimit());
                    sb.append("\nra_mflag: ");
                    sb.append(ra.getMFlag());
                    sb.append("\nra_oflag: ");
                    sb.append(ra.getOFlag());
                    sb.append("\nra_reachable_time: ");
                    sb.append(ra.getReachableTime());
                    sb.append("\nra_retransmit_time: ");
                    sb.append(ra.getRetransmitTimer());
                    sb.append("\nra_router_lifetime: ");
                    sb.append(ra.getRouterLifetime());
                    ra.getOptions().forEach(option -> {
                        sb.append("\noption_type: ");
                        sb.append(option.type());
                        sb.append("\noption_data: ");
                        sb.append(bytesToHex(option.data()));
                    });
                } else if (payload instanceof Redirect) {
                    final Redirect rd = (Redirect) payload;
                    sb.append("\nrd_target_addr: ");
                    sb.append(Ip6Address.valueOf(rd.getTargetAddress()).toString());
                    rd.getOptions().forEach(option -> {
                        sb.append("\noption_type: ");
                        sb.append(option.type());
                        sb.append("\noption_data: ");
                        sb.append(bytesToHex(option.data()));
                    });
                }
            }
        }
    } else if (pkt instanceof DHCP) {
        sb.append("\ndhcp packet");
    } else if (pkt instanceof Data) {
        sb.append("\ndata packet");
    } else if (pkt instanceof LLC) {
        sb.append("\nllc packet");
    } else if (pkt instanceof EAPOL) {
        sb.append("\neapol");
    } else if (pkt instanceof PPPoED) {
        sb.append("\npppoed packet");
    } else {
        sb.append("\nunknown packet");
    }
    return sb.toString();
}
Also used : Arrays(java.util.Arrays) ImmutableMap(com.google.common.collect.ImmutableMap) RouterAdvertisement(org.onlab.packet.ndp.RouterAdvertisement) RouterSolicitation(org.onlab.packet.ndp.RouterSolicitation) Map(java.util.Map) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) PacketUtils.checkHeaderLength(org.onlab.packet.PacketUtils.checkHeaderLength) Redirect(org.onlab.packet.ndp.Redirect) PacketUtils.checkInput(org.onlab.packet.PacketUtils.checkInput) NeighborAdvertisement(org.onlab.packet.ndp.NeighborAdvertisement) ByteBuffer(java.nio.ByteBuffer) NeighborSolicitation(org.onlab.packet.ndp.NeighborSolicitation) RouterSolicitation(org.onlab.packet.ndp.RouterSolicitation) Redirect(org.onlab.packet.ndp.Redirect) NeighborAdvertisement(org.onlab.packet.ndp.NeighborAdvertisement) NeighborSolicitation(org.onlab.packet.ndp.NeighborSolicitation) RouterAdvertisement(org.onlab.packet.ndp.RouterAdvertisement)

Example 4 with NeighborSolicitation

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

Aggregations

NeighborSolicitation (org.onlab.packet.ndp.NeighborSolicitation)4 IPv6 (org.onlab.packet.IPv6)3 Test (org.junit.Test)2 Ethernet (org.onlab.packet.Ethernet)2 ConnectPoint (org.onosproject.net.ConnectPoint)2 Device (org.onosproject.net.Device)2 DeviceId (org.onosproject.net.DeviceId)2 Port (org.onosproject.net.Port)2 PortNumber (org.onosproject.net.PortNumber)2 Instruction (org.onosproject.net.flow.instructions.Instruction)2 OutputInstruction (org.onosproject.net.flow.instructions.Instructions.OutputInstruction)2 Interface (org.onosproject.net.intf.Interface)2 InterfaceService (org.onosproject.net.intf.InterfaceService)2 OutboundPacket (org.onosproject.net.packet.OutboundPacket)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ByteBuffer (java.nio.ByteBuffer)1 Arrays (java.util.Arrays)1 Map (java.util.Map)1 ICMP6 (org.onlab.packet.ICMP6)1