Java Code Examples for org.onlab.packet.Ethernet#setVlanID()

The following examples show how to use org.onlab.packet.Ethernet#setVlanID() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EthernetCodecTest.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Unit test for the ethernet object codec.
 */
@Test
public void ethernetCodecTest() {
    final CodecContext context = new MockCodecContext();
    final JsonCodec<Ethernet> ethernetCodec = context.codec(Ethernet.class);
    assertThat(ethernetCodec, notNullValue());

    final Ethernet eth1 = new Ethernet();
    eth1.setSourceMACAddress("11:22:33:44:55:01");
    eth1.setDestinationMACAddress("11:22:33:44:55:02");
    eth1.setPad(true);
    eth1.setEtherType(Ethernet.TYPE_ARP);
    eth1.setPriorityCode((byte) 7);
    eth1.setVlanID((short) 33);

    final ObjectNode eth1Json = ethernetCodec.encode(eth1, context);
    assertThat(eth1Json, notNullValue());
    assertThat(eth1Json, matchesEthernet(eth1));
}
 
Example 2
Source File: DefaultNeighbourMessageActionsTest.java    From onos with Apache License 2.0 6 votes vote down vote up
@Test
public void forwardToInterface() {
    Ethernet request = NeighbourTestUtils.createArpRequest(IP1);

    Ethernet forwardedRequest = request.duplicate();
    forwardedRequest.setSourceMACAddress(INTF2.mac());
    forwardedRequest.setVlanID(INTF2.vlan().toShort());

    packetService.emit(outbound(forwardedRequest, CP2));
    expectLastCall().once();
    replay(packetService);

    actions.forward(createContext(request, CP1, null), INTF2);

    verify(packetService);
}
 
Example 3
Source File: DirectHostManager.java    From onos with Apache License 2.0 6 votes vote down vote up
private void transformAndSend(IP ip, short ethType,
                              Interface egressInterface,
                              MacAddress macAddress) {
    // Base processing for IPv4
    if (ethType == Ethernet.TYPE_IPV4) {
        IPv4 ipv4 = (IPv4) ip;
        ipv4.setTtl((byte) (ipv4.getTtl() - 1));
        ipv4.setChecksum((short) 0);
    // Base processing for IPv6.
    } else {
        IPv6 ipv6 = (IPv6) ip;
        ipv6.setHopLimit((byte) (ipv6.getHopLimit() - 1));
        ipv6.resetChecksum();
    }
    // Sends and serializes.
    Ethernet eth = new Ethernet();
    eth.setDestinationMACAddress(macAddress);
    eth.setSourceMACAddress(egressInterface.mac());
    eth.setEtherType(ethType);
    eth.setPayload(ip);
    if (!egressInterface.vlan().equals(VlanId.NONE)) {
        eth.setVlanID(egressInterface.vlan().toShort());
    }
    send(eth, egressInterface.connectPoint());
}
 
Example 4
Source File: DhcpManager.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Processes the ARP Payload and initiates a reply to the client.
 *
 * @param context context of the incoming message
 * @param packet the ethernet payload
 */
private void processArpPacket(PacketContext context, Ethernet packet) {

    ARP arpPacket = (ARP) packet.getPayload();

    ARP arpReply = arpPacket.duplicate();
    arpReply.setOpCode(ARP.OP_REPLY);

    arpReply.setTargetProtocolAddress(arpPacket.getSenderProtocolAddress());
    arpReply.setTargetHardwareAddress(arpPacket.getSenderHardwareAddress());
    arpReply.setSenderProtocolAddress(arpPacket.getTargetProtocolAddress());
    arpReply.setSenderHardwareAddress(myMAC.toBytes());

    // Ethernet Frame.
    Ethernet ethReply = new Ethernet();
    ethReply.setSourceMACAddress(myMAC);
    ethReply.setDestinationMACAddress(packet.getSourceMAC());
    ethReply.setEtherType(Ethernet.TYPE_ARP);
    ethReply.setVlanID(packet.getVlanID());

    ethReply.setPayload(arpReply);
    sendReply(context, ethReply);
}
 
Example 5
Source File: DhcpRelayManagerTest.java    From onos with Apache License 2.0 6 votes vote down vote up
public TestArpRequestPacketContext(Interface fromInterface) {
    super(0, null, null, false);
    ARP arp = new ARP();
    arp.setOpCode(ARP.OP_REQUEST);

    IpAddress targetIp = fromInterface.ipAddressesList().get(0).ipAddress();
    arp.setTargetProtocolAddress(targetIp.toOctets());
    arp.setTargetHardwareAddress(MacAddress.BROADCAST.toBytes());
    arp.setSenderHardwareAddress(MacAddress.NONE.toBytes());
    arp.setSenderProtocolAddress(Ip4Address.valueOf(0).toOctets());
    arp.setHardwareAddressLength((byte) MacAddress.MAC_ADDRESS_LENGTH);
    Ethernet eth = new Ethernet();
    eth.setEtherType(Ethernet.TYPE_ARP);
    eth.setSourceMACAddress(MacAddress.NONE);
    eth.setDestinationMACAddress(MacAddress.BROADCAST);
    eth.setVlanID(fromInterface.vlan().toShort());
    eth.setPayload(arp);

    this.inPacket = new DefaultInboundPacket(fromInterface.connectPoint(), eth,
                                             ByteBuffer.wrap(eth.serialize()));
}
 
Example 6
Source File: OpenstackNetworkingUtil.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Returns GARP packet with supplied floating ip and instance port information.
 *
 * @param floatingIP floating ip
 * @param instancePort instance port
 * @param vlanId vlan id
 * @return GARP packet
 */
private static Ethernet buildGratuitousArpPacket(NetFloatingIP floatingIP,
                                                 InstancePort instancePort,
                                                 VlanId vlanId) {
    Ethernet ethernet = new Ethernet();
    ethernet.setDestinationMACAddress(MacAddress.BROADCAST);
    ethernet.setSourceMACAddress(instancePort.macAddress());
    ethernet.setEtherType(Ethernet.TYPE_ARP);
    ethernet.setVlanID(vlanId.id());

    ARP arp = new ARP();
    arp.setOpCode(ARP.OP_REPLY);
    arp.setProtocolType(ARP.PROTO_TYPE_IP);
    arp.setHardwareType(ARP.HW_TYPE_ETHERNET);

    arp.setProtocolAddressLength((byte) Ip4Address.BYTE_LENGTH);
    arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH);

    arp.setSenderHardwareAddress(instancePort.macAddress().toBytes());
    arp.setTargetHardwareAddress(MacAddress.BROADCAST.toBytes());

    arp.setSenderProtocolAddress(valueOf(floatingIP.getFloatingIpAddress()).toInt());
    arp.setTargetProtocolAddress(valueOf(floatingIP.getFloatingIpAddress()).toInt());

    ethernet.setPayload(arp);

    return ethernet;
}
 
Example 7
Source File: HostMonitor.java    From onos with Apache License 2.0 5 votes vote down vote up
private Ethernet buildArpRequest(IpAddress targetIp, IpAddress sourceIp,
                                 MacAddress sourceMac, VlanId vlan) {

    ARP arp = new ARP();
    arp.setHardwareType(ARP.HW_TYPE_ETHERNET)
       .setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH)
       .setProtocolType(ARP.PROTO_TYPE_IP)
       .setProtocolAddressLength((byte) IpAddress.INET_BYTE_LENGTH)
       .setOpCode(ARP.OP_REQUEST);

    arp.setSenderHardwareAddress(sourceMac.toBytes())
       .setSenderProtocolAddress(sourceIp.toOctets())
       .setTargetHardwareAddress(ZERO_MAC_ADDRESS)
       .setTargetProtocolAddress(targetIp.toOctets());

    Ethernet ethernet = new Ethernet();
    ethernet.setEtherType(Ethernet.TYPE_ARP)
            .setDestinationMACAddress(MacAddress.BROADCAST)
            .setSourceMACAddress(sourceMac)
            .setPayload(arp);

    if (!vlan.equals(VlanId.NONE)) {
        ethernet.setVlanID(vlan.toShort());
    }

    ethernet.setPad(true);

    return ethernet;
}
 
Example 8
Source File: DefaultNeighbourMessageActions.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * 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
 * @param isRouter true if this reply is sent on behalf of a router
 * @return an Ethernet frame containing the Neighbor Advertisement reply
 */
private Ethernet buildNdpReply(Ip6Address srcIp, MacAddress srcMac,
                               Ethernet request, boolean isRouter) {
    Ethernet eth = new Ethernet();
    eth.setDestinationMACAddress(request.getSourceMAC());
    eth.setSourceMACAddress(srcMac);
    eth.setEtherType(Ethernet.TYPE_IPV6);
    eth.setVlanID(request.getVlanID());

    IPv6 requestIp = (IPv6) request.getPayload();
    IPv6 ipv6 = new IPv6();
    ipv6.setSourceAddress(srcIp.toOctets());
    ipv6.setDestinationAddress(requestIp.getSourceAddress());
    ipv6.setHopLimit((byte) 255);

    ICMP6 icmp6 = new ICMP6();
    icmp6.setIcmpType(ICMP6.NEIGHBOR_ADVERTISEMENT);
    icmp6.setIcmpCode((byte) 0);

    NeighborAdvertisement nadv = new NeighborAdvertisement();
    nadv.setTargetAddress(srcIp.toOctets());
    nadv.setSolicitedFlag((byte) 1);
    nadv.setOverrideFlag((byte) 1);
    if (isRouter) {
        nadv.setRouterFlag((byte) 1);
    }
    nadv.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
            srcMac.toBytes());

    icmp6.setPayload(nadv);
    ipv6.setPayload(icmp6);
    eth.setPayload(ipv6);
    return eth;
}
 
Example 9
Source File: DefaultNeighbourMessageActions.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public void forward(NeighbourMessageContext context, Interface outIntf) {
    Ethernet packetOut = context.packet().duplicate();
    if (outIntf.vlan().equals(VlanId.NONE)) {
        // The egress interface has no VLAN Id. Send out an untagged
        // packet
        packetOut.setVlanID(Ethernet.VLAN_UNTAGGED);
    } else {
        // The egress interface has a VLAN set. Send out a tagged packet
        packetOut.setVlanID(outIntf.vlan().toShort());
    }
    sendTo(packetOut, outIntf.connectPoint());
}
 
Example 10
Source File: DefaultVirtualPacketProviderTest.java    From onos with Apache License 2.0 5 votes vote down vote up
/** Test the physical packet context is delivered to a proper (physical)
 *  virtual network and device.
 */
@Test
public void virtualizePacket() {
    Ethernet eth = new Ethernet();
    eth.setSourceMACAddress(SRC_MAC_ADDR);
    eth.setDestinationMACAddress(DST_MAC_ADDR);
    eth.setVlanID((short) 1);
    eth.setPayload(null);

    InboundPacket pInPacket =
            new DefaultInboundPacket(CP22, eth,
                                     ByteBuffer.wrap(eth.serialize()));

    PacketContext pContext =
            new TestPacketContext(System.nanoTime(), pInPacket, null, false);

    testPacketService.sendTestPacketContext(pContext);

    PacketContext vContext = providerService.getRequestedPacketContext(0);
    InboundPacket vInPacket = vContext.inPacket();

    assertEquals("the packet should be received from VCP12",
                 VCP12, vInPacket.receivedFrom());

    assertEquals("VLAN tag should be excludede", VlanId.UNTAGGED,
                 vInPacket.parsed().getVlanID());
}
 
Example 11
Source File: OpenstackRoutingSnatIcmpHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
private void sendRequestForExternal(IPv4 ipPacket,
                                    DeviceId srcDevice,
                                    IpAddress srcNatIp,
                                    ExternalPeerRouter externalPeerRouter) {
    ICMP icmpReq = (ICMP) ipPacket.getPayload();
    icmpReq.resetChecksum();
    ipPacket.setSourceAddress(srcNatIp.getIp4Address().toInt()).resetChecksum();
    ipPacket.setPayload(icmpReq);

    Ethernet icmpRequestEth = new Ethernet();
    icmpRequestEth.setEtherType(Ethernet.TYPE_IPV4)
            .setSourceMACAddress(DEFAULT_GATEWAY_MAC)
            .setDestinationMACAddress(externalPeerRouter.macAddress());

    if (!externalPeerRouter.vlanId().equals(VlanId.NONE)) {
        icmpRequestEth.setVlanID(externalPeerRouter.vlanId().toShort());
    }

    icmpRequestEth.setPayload(ipPacket);

    OpenstackNode osNode = osNodeService.node(srcDevice);
    if (osNode == null) {
        final String error = String.format("Cannot find openstack node for %s",
                srcDevice);
        throw new IllegalStateException(error);
    }
    TrafficTreatment treatment = DefaultTrafficTreatment.builder()
            .setOutput(osNode.uplinkPortNum())
            .build();

    OutboundPacket packet = new DefaultOutboundPacket(
            srcDevice,
            treatment,
            ByteBuffer.wrap(icmpRequestEth.serialize()));

    packetService.emit(packet);
}
 
Example 12
Source File: DhcpRelayManager.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Processes the ARP Payload and initiates a reply to the client.
 *
 * @param context the packet context
 * @param packet the ethernet payload
 * @param replyMac mac address to be replied
 */
private void processArpPacket(PacketContext context, Ethernet packet, MacAddress replyMac) {
    ARP arpPacket = (ARP) packet.getPayload();
    ARP arpReply = arpPacket.duplicate();
    arpReply.setOpCode(ARP.OP_REPLY);

    arpReply.setTargetProtocolAddress(arpPacket.getSenderProtocolAddress());
    arpReply.setTargetHardwareAddress(arpPacket.getSenderHardwareAddress());
    arpReply.setSenderProtocolAddress(arpPacket.getTargetProtocolAddress());
    arpReply.setSenderHardwareAddress(replyMac.toBytes());

    // Ethernet Frame.
    Ethernet ethReply = new Ethernet();
    ethReply.setSourceMACAddress(replyMac.toBytes());
    ethReply.setDestinationMACAddress(packet.getSourceMAC());
    ethReply.setEtherType(Ethernet.TYPE_ARP);
    ethReply.setVlanID(packet.getVlanID());
    ethReply.setPayload(arpReply);

    ConnectPoint targetPort = context.inPacket().receivedFrom();
    TrafficTreatment t = DefaultTrafficTreatment.builder()
            .setOutput(targetPort.port()).build();
    OutboundPacket o = new DefaultOutboundPacket(
            targetPort.deviceId(), t, ByteBuffer.wrap(ethReply.serialize()));
    if (log.isTraceEnabled()) {
        log.trace("Relaying ARP packet {} to {}", packet, targetPort);
    }
    packetService.emit(o);
}
 
Example 13
Source File: IcmpHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
private void sendIcmpResponse(Ethernet icmpRequest, ConnectPoint outport) {

        Ethernet icmpReplyEth = new Ethernet();

        IPv4 icmpRequestIpv4 = (IPv4) icmpRequest.getPayload();
        IPv4 icmpReplyIpv4 = new IPv4();

        int destAddress = icmpRequestIpv4.getDestinationAddress();
        icmpReplyIpv4.setDestinationAddress(icmpRequestIpv4.getSourceAddress());
        icmpReplyIpv4.setSourceAddress(destAddress);
        icmpReplyIpv4.setTtl((byte) 64);
        icmpReplyIpv4.setChecksum((short) 0);

        ICMP icmpReply = new ICMP();
        icmpReply.setPayload(((ICMP) icmpRequestIpv4.getPayload()).getPayload());
        icmpReply.setIcmpType(ICMP.TYPE_ECHO_REPLY);
        icmpReply.setIcmpCode(ICMP.CODE_ECHO_REPLY);
        icmpReply.setChecksum((short) 0);

        icmpReplyIpv4.setPayload(icmpReply);

        icmpReplyEth.setPayload(icmpReplyIpv4);
        icmpReplyEth.setEtherType(Ethernet.TYPE_IPV4);
        icmpReplyEth.setDestinationMACAddress(icmpRequest.getSourceMACAddress());
        icmpReplyEth.setSourceMACAddress(icmpRequest.getDestinationMACAddress());
        icmpReplyEth.setVlanID(icmpRequest.getVlanID());

        sendPacketOut(outport, icmpReplyEth);

    }
 
Example 14
Source File: OpenstackRoutingSnatHandler.java    From onos with Apache License 2.0 4 votes vote down vote up
private void packetOut(Ethernet ethPacketIn, DeviceId srcDevice, int patPort,
                       IpAddress externalIp, ExternalPeerRouter externalPeerRouter) {
    IPv4 iPacket = (IPv4) ethPacketIn.getPayload();
    switch (iPacket.getProtocol()) {
        case IPv4.PROTOCOL_TCP:
            iPacket.setPayload(buildPacketOutTcp(iPacket, patPort));
            break;
        case IPv4.PROTOCOL_UDP:
            iPacket.setPayload(buildPacketOutUdp(iPacket, patPort));
            break;
        default:
            log.trace("Temporally, this method can process UDP and TCP protocol.");
            return;
    }

    iPacket.setSourceAddress(externalIp.toString());
    iPacket.resetChecksum();
    iPacket.setParent(ethPacketIn);
    ethPacketIn.setSourceMACAddress(DEFAULT_GATEWAY_MAC);
    ethPacketIn.setDestinationMACAddress(externalPeerRouter.macAddress());
    ethPacketIn.setPayload(iPacket);

    if (!externalPeerRouter.vlanId().equals(VlanId.NONE)) {
        ethPacketIn.setVlanID(externalPeerRouter.vlanId().toShort());
    }

    ethPacketIn.resetChecksum();

    OpenstackNode srcNode = osNodeService.node(srcDevice);
    if (srcNode == null) {
        final String error = String.format("Cannot find openstack node for %s",
                srcDevice);
        throw new IllegalStateException(error);
    }

    TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();

    packetService.emit(new DefaultOutboundPacket(
            srcDevice,
            tBuilder.setOutput(srcNode.uplinkPortNum()).build(),
            ByteBuffer.wrap(ethPacketIn.serialize())));
}
 
Example 15
Source File: NeighborSolicitation.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Builds a NDP solicitation using the supplied parameters.
 *
 * @param targetIp the target ip
 * @param sourceIp the source ip
 * @param destinationIp the destination ip
 * @param sourceMac the source mac address
 * @param destinationMac the destination mac address
 * @param vlan the vlan id
 * @return the ethernet packet containing the ndp solicitation
 */
public static Ethernet buildNdpSolicit(Ip6Address targetIp,
                                       Ip6Address sourceIp,
                                       Ip6Address destinationIp,
                                       MacAddress sourceMac,
                                       MacAddress destinationMac,
                                       VlanId vlan) {

    checkNotNull(targetIp, "Target IP address cannot be null");
    checkNotNull(sourceIp, "Source IP address cannot be null");
    checkNotNull(destinationIp, "Destination IP address cannot be null");
    checkNotNull(sourceMac, "Source MAC address cannot be null");
    checkNotNull(destinationMac, "Destination MAC address cannot be null");
    checkNotNull(vlan, "Vlan cannot be null");

    // Here we craft the Ethernet packet.
    Ethernet ethernet = new Ethernet();
    ethernet.setEtherType(Ethernet.TYPE_IPV6)
            .setDestinationMACAddress(destinationMac)
            .setSourceMACAddress(sourceMac);
    ethernet.setVlanID(vlan.id());
    // IPv6 packet is created.
    IPv6 ipv6 = new IPv6();
    ipv6.setSourceAddress(sourceIp.toOctets());
    ipv6.setDestinationAddress(destinationIp.toOctets());
    ipv6.setHopLimit(NDP_HOP_LIMIT);
    // Create the ICMPv6 packet.
    ICMP6 icmp6 = new ICMP6();
    icmp6.setIcmpType(ICMP6.NEIGHBOR_SOLICITATION);
    icmp6.setIcmpCode(RESERVED_CODE);
    // Create the Neighbor Solicitation packet.
    NeighborSolicitation ns = new NeighborSolicitation();
    ns.setTargetAddress(targetIp.toOctets());
    // DAD packets should not contain SRC_LL_ADDR option
    if (!Arrays.equals(sourceIp.toOctets(), Ip6Address.ZERO.toOctets())) {
        ns.addOption(NeighborDiscoveryOptions.TYPE_SOURCE_LL_ADDRESS, sourceMac.toBytes());
    }
    // Set the payloads
    icmp6.setPayload(ns);
    ipv6.setPayload(icmp6);
    ethernet.setPayload(ipv6);

    return ethernet;
}
 
Example 16
Source File: DhcpManagerTest.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Constructs an Ethernet packet containing a DHCP Payload.
 * @param packetType DHCP Message Type
 * @return Ethernet packet
 */
private Ethernet constructDhcpPacket(DHCP.MsgType packetType) {

    // Ethernet Frame.
    Ethernet ethReply = new Ethernet();
    ethReply.setSourceMACAddress(CLIENT1_HOST.mac());
    ethReply.setDestinationMACAddress(MacAddress.BROADCAST);
    ethReply.setEtherType(Ethernet.TYPE_IPV4);
    ethReply.setVlanID((short) 2);

    // IP Packet
    IPv4 ipv4Reply = new IPv4();
    ipv4Reply.setSourceAddress(0);
    ipv4Reply.setDestinationAddress(BROADCAST.toInt());
    ipv4Reply.setTtl((byte) 127);

    // UDP Datagram.
    UDP udpReply = new UDP();
    udpReply.setSourcePort((byte) UDP.DHCP_CLIENT_PORT);
    udpReply.setDestinationPort((byte) UDP.DHCP_SERVER_PORT);

    // DHCP Payload.
    DHCP dhcpReply = new DHCP();
    dhcpReply.setOpCode(DHCP.OPCODE_REQUEST);

    dhcpReply.setYourIPAddress(0);
    dhcpReply.setServerIPAddress(0);

    dhcpReply.setTransactionId(TRANSACTION_ID);
    dhcpReply.setClientHardwareAddress(CLIENT1_HOST.mac().toBytes());
    dhcpReply.setHardwareType(DHCP.HWTYPE_ETHERNET);
    dhcpReply.setHardwareAddressLength((byte) 6);

    // DHCP Options.
    DhcpOption option = new DhcpOption();
    List<DhcpOption> optionList = new ArrayList<>();

    // DHCP Message Type.
    option.setCode(DHCP.DHCPOptionCode.OptionCode_MessageType.getValue());
    option.setLength((byte) 1);
    byte[] optionData = {(byte) packetType.getValue()};
    option.setData(optionData);
    optionList.add(option);

    // DHCP Requested IP.
    option = new DhcpOption();
    option.setCode(DHCP.DHCPOptionCode.OptionCode_RequestedIP.getValue());
    option.setLength((byte) 4);
    optionData = Ip4Address.valueOf(EXPECTED_IP).toOctets();
    option.setData(optionData);
    optionList.add(option);

    // End Option.
    option = new DhcpOption();
    option.setCode(DHCP.DHCPOptionCode.OptionCode_END.getValue());
    option.setLength((byte) 1);
    optionList.add(option);

    dhcpReply.setOptions(optionList);

    udpReply.setPayload(dhcpReply);
    ipv4Reply.setPayload(udpReply);
    ethReply.setPayload(ipv4Reply);

    return ethReply;
}
 
Example 17
Source File: NeighborAdvertisement.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
}