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

The following examples show how to use org.onlab.packet.Ethernet#setEtherType() . 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: OpenstackMetadataProxyHandler.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Builds an ethernet frame with the given IPv4 and TCP payload.
 *
 * @param ethRequest    ethernet request frame
 * @param ipv4Request   IPv4 request
 * @param tcpReply      TCP reply
 * @return an ethernet frame contains TCP payload
 */
private Ethernet buildEthFrame(Ethernet ethRequest, IPv4 ipv4Request,
                               TCP tcpReply) {
    Ethernet ethReply = new Ethernet();
    ethReply.setSourceMACAddress(ethRequest.getDestinationMAC());
    ethReply.setDestinationMACAddress(ethRequest.getSourceMAC());
    ethReply.setEtherType(ethRequest.getEtherType());

    IPv4 ipv4Reply = new IPv4();
    ipv4Reply.setSourceAddress(ipv4Request.getDestinationAddress());
    ipv4Reply.setDestinationAddress(ipv4Request.getSourceAddress());
    ipv4Reply.setTtl(PACKET_TTL);

    ipv4Reply.setPayload(tcpReply);
    ethReply.setPayload(ipv4Reply);

    return ethReply;
}
 
Example 2
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 3
Source File: MQEventHandlerTest.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public InboundPacket inPacket() {
    ONOSLLDP lldp = ONOSLLDP.onosSecureLLDP(deviceService.getDevice(DID1)
                                      .id().toString(),
                                            device.chassisId(),
                                            (int) pd1.number().toLong(), "", "test");

    Ethernet ethPacket = new Ethernet();
    ethPacket.setEtherType(Ethernet.TYPE_LLDP);
    ethPacket.setDestinationMACAddress(MacAddress.ONOS_LLDP);
    ethPacket.setPayload(lldp);
    ethPacket.setPad(true);

    ethPacket.setSourceMACAddress("DE:AD:BE:EF:BA:11");

    ConnectPoint cp = new ConnectPoint(device.id(), pd3.number());

    return new DefaultInboundPacket(cp, ethPacket,
                                    ByteBuffer.wrap(ethPacket
                                    .serialize()));

}
 
Example 4
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 5
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 6
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 7
Source File: LinkDiscovery.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Instantiates discovery manager for the given physical switch. Creates a
 * generic LLDP packet that will be customized for the port it is sent out on.
 * Starts the the timer for the discovery process.
 *
 * @param deviceId  the physical switch
 * @param context discovery context
 */
public LinkDiscovery(DeviceId deviceId, LinkDiscoveryContext context) {
    this.deviceId = deviceId;
    this.context = context;

    ethPacket = new Ethernet();
    ethPacket.setEtherType(Ethernet.TYPE_LLDP);
    ethPacket.setDestinationMACAddress(MacAddress.ONOS_LLDP);
    ethPacket.setPad(true);

    bddpEth = new Ethernet();
    bddpEth.setEtherType(Ethernet.TYPE_BSN);
    bddpEth.setDestinationMACAddress(MacAddress.BROADCAST);
    bddpEth.setPad(true);

    isStopped = true;
    start();
    log.debug("Started discovery manager for switch {}", deviceId);

}
 
Example 8
Source File: LldpLinkProviderTest.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public InboundPacket inPacket() {
    ONOSLLDP lldp = ONOSLLDP.onosSecureLLDP(deviceService.getDevice(DID1).id().toString(),
                                            device.chassisId(),
                                            (int) pd1.number().toLong(), "", "test");

    Ethernet ethPacket = new Ethernet();
    ethPacket.setEtherType(Ethernet.TYPE_LLDP);
    ethPacket.setDestinationMACAddress(MacAddress.ONOS_LLDP);
    ethPacket.setPayload(lldp);
    ethPacket.setPad(true);

    ethPacket.setSourceMACAddress("DE:AD:BE:EF:BA:11");

    ConnectPoint cp = new ConnectPoint(device.id(), pd3.number());

    return new DefaultInboundPacket(cp, ethPacket,
                                    ByteBuffer.wrap(ethPacket.serialize()));

}
 
Example 9
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 10
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 11
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 12
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 13
Source File: OpenstackMetadataProxyHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Builds a set of ethernet frames with the given IPv4 and TCP payload.
 *
 * @param ethRequest    ethernet request frame
 * @param ipv4Request   IPv4 request
 * @param tcpReplies      TCP replies
 * @return a set of ethernet frames
 */
private List<Ethernet> buildEthFrames(Ethernet ethRequest,
                                      IPv4 ipv4Request,
                                      List<TCP> tcpReplies) {

    List<Ethernet> ethReplies = Lists.newArrayList();

    for (TCP tcpReply : tcpReplies) {

        Ethernet ethReply = new Ethernet();
        ethReply.setSourceMACAddress(ethRequest.getDestinationMAC());
        ethReply.setDestinationMACAddress(ethRequest.getSourceMAC());
        ethReply.setEtherType(ethRequest.getEtherType());

        IPv4 ipv4Reply = new IPv4();
        ipv4Reply.setSourceAddress(ipv4Request.getDestinationAddress());
        ipv4Reply.setDestinationAddress(ipv4Request.getSourceAddress());
        ipv4Reply.setTtl(PACKET_TTL);

        ipv4Reply.setProtocol(IPv4.PROTOCOL_TCP);
        ipv4Reply.setPayload(tcpReply);

        ethReply.setPayload(ipv4Reply);

        ethReplies.add(ethReply);
    }

    return ethReplies;
}
 
Example 14
Source File: IpHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Forwards IP packets in the buffer to the destination IP address.
 * It is called when the controller finds the destination MAC address
 * via ARP responses.
 *
 * @param deviceId switch device ID
 * @param destIpAddress destination IP address
 */
public void forwardPackets(DeviceId deviceId, Ip4Address destIpAddress) {
    if (ipPacketQueue.get(destIpAddress) == null) {
        return;
    }
    for (IP ipPacket : ipPacketQueue.get(destIpAddress)) {
        if (ipPacket.getVersion() == ((byte) 4)) {
            IPv4 ipv4Packet = (IPv4) ipPacket;
            Ip4Address destAddress = Ip4Address.valueOf(ipv4Packet.getDestinationAddress());
            if (config.inSameSubnet(deviceId, destAddress)) {
                ipv4Packet.setTtl((byte) (ipv4Packet.getTtl() - 1));
                ipv4Packet.setChecksum((short) 0);
                for (Host dest : srManager.hostService.getHostsByIp(destIpAddress)) {
                    Ethernet eth = new Ethernet();
                    eth.setDestinationMACAddress(dest.mac());
                    try {
                        eth.setSourceMACAddress(config.getDeviceMac(deviceId));
                    } catch (DeviceConfigNotFoundException e) {
                        log.warn(e.getMessage()
                                         + " Skipping forwardPackets for this destination.");
                        continue;
                    }
                    eth.setEtherType(Ethernet.TYPE_IPV4);
                    eth.setPayload(ipv4Packet);
                    forwardToHost(deviceId, eth, dest);
                    ipPacketQueue.get(destIpAddress).remove(ipPacket);
                }
                ipPacketQueue.get(destIpAddress).remove(ipPacket);
            }
        }
    }
}
 
Example 15
Source File: NeighborAdvertisementTest.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * 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())));
}
 
Example 16
Source File: Dhcp6Test.java    From onos with Apache License 2.0 4 votes vote down vote up
@Test
public void serializeReply() throws Exception {
    DHCP6 dhcp6 = new DHCP6();
    dhcp6.setMsgType(DHCP6.MsgType.REPLY.value());
    dhcp6.setTransactionId(XID_2);
    List<Dhcp6Option> options = Lists.newArrayList();

    // IA address
    Dhcp6IaAddressOption iaAddressOption = new Dhcp6IaAddressOption();
    iaAddressOption.setIp6Address(IA_ADDRESS);
    iaAddressOption.setPreferredLifetime(PREFFERRED_LT_SERVER);
    iaAddressOption.setValidLifetime(VALID_LT_SERVER);

    // IA NA
    Dhcp6IaNaOption iaNaOption = new Dhcp6IaNaOption();
    iaNaOption.setIaId(IA_ID);
    iaNaOption.setT1(T1_SERVER);
    iaNaOption.setT2(T2_SERVER);
    iaNaOption.setOptions(ImmutableList.of(iaAddressOption));
    options.add(iaNaOption);

    // Client ID
    Dhcp6Duid duid = new Dhcp6Duid();
    duid.setDuidType(Dhcp6Duid.DuidType.DUID_LLT);
    duid.setHardwareType((short) 1);
    duid.setDuidTime(CLIENT_DUID_TIME);
    duid.setLinkLayerAddress(CLIENT_MAC.toBytes());
    Dhcp6ClientIdOption clientIdOption = new Dhcp6ClientIdOption();
    clientIdOption.setDuid(duid);
    options.add(clientIdOption);

    // Server ID
    Dhcp6Option option = new Dhcp6Option();
    option.setCode(DHCP6.OptionCode.SERVERID.value());
    option.setLength((short) 14);
    Dhcp6Duid serverDuid = new Dhcp6Duid();
    serverDuid.setDuidType(Dhcp6Duid.DuidType.DUID_LLT);
    serverDuid.setLinkLayerAddress(SERVER_MAC.toBytes());
    serverDuid.setHardwareType((short) 1);
    serverDuid.setDuidTime(0x211e5340);
    option.setData(serverDuid.serialize());
    options.add(option);

    dhcp6.setOptions(options);

    Dhcp6RelayOption relayOption = new Dhcp6RelayOption();
    relayOption.setPayload(dhcp6);

    UDP udp = new UDP();
    udp.setSourcePort(UDP.DHCP_V6_SERVER_PORT);
    udp.setDestinationPort(UDP.DHCP_V6_CLIENT_PORT);
    udp.setPayload(dhcp6);
    udp.setChecksum((short) 0xcb5a);

    IPv6 ipv6 = new IPv6();
    ipv6.setHopLimit((byte) 64);
    ipv6.setSourceAddress(UPSTREAM_LL.toOctets());
    ipv6.setDestinationAddress(CLIENT_LL.toOctets());
    ipv6.setNextHeader(IPv6.PROTOCOL_UDP);
    ipv6.setTrafficClass((byte) 0);
    ipv6.setFlowLabel(0x000d935f);
    ipv6.setPayload(udp);

    Ethernet eth = new Ethernet();
    eth.setDestinationMACAddress(CLIENT_MAC);
    eth.setSourceMACAddress(UPSTREAM_MAC);
    eth.setEtherType(Ethernet.TYPE_IPV6);
    eth.setPayload(ipv6);

    assertArrayEquals(Resources.toByteArray(Dhcp6RelayTest.class.getResource(REPLY)),
                      eth.serialize());
}
 
Example 17
Source File: Dhcp6Test.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Test serialize request message.
 *
 * @throws Exception exception while serialize the DHCPv6 payload
 */
@Test
public void serializeRequest() throws Exception {
    DHCP6 dhcp6 = new DHCP6();
    dhcp6.setMsgType(DHCP6.MsgType.REQUEST.value());
    dhcp6.setTransactionId(XID_2);
    List<Dhcp6Option> options = Lists.newArrayList();

    // Client ID
    Dhcp6Duid duid = new Dhcp6Duid();
    duid.setDuidType(Dhcp6Duid.DuidType.DUID_LLT);
    duid.setHardwareType((short) 1);
    duid.setDuidTime(CLIENT_DUID_TIME);
    duid.setLinkLayerAddress(CLIENT_MAC.toBytes());
    Dhcp6ClientIdOption clientIdOption = new Dhcp6ClientIdOption();
    clientIdOption.setDuid(duid);
    options.add(clientIdOption);

    // Server ID
    Dhcp6Option option = new Dhcp6Option();
    option.setCode(DHCP6.OptionCode.SERVERID.value());
    option.setLength((short) 14);
    Dhcp6Duid serverDuid = new Dhcp6Duid();
    serverDuid.setDuidType(Dhcp6Duid.DuidType.DUID_LLT);
    serverDuid.setLinkLayerAddress(SERVER_MAC.toBytes());
    serverDuid.setHardwareType((short) 1);
    serverDuid.setDuidTime(0x211e5340);
    option.setData(serverDuid.serialize());
    options.add(option);

    // Option request
    option = new Dhcp6Option();
    option.setCode(DHCP6.OptionCode.ORO.value());
    option.setLength((short) 8);
    option.setData(new byte[]{0, 23, 0, 24, 0, 39, 0, 31});
    options.add(option);

    // Elapsed Time
    option = new Dhcp6Option();
    option.setCode(DHCP6.OptionCode.ELAPSED_TIME.value());
    option.setLength((short) 2);
    option.setData(new byte[]{0, 0});
    options.add(option);

    // IA address
    Dhcp6IaAddressOption iaAddressOption = new Dhcp6IaAddressOption();
    iaAddressOption.setIp6Address(IA_ADDRESS);
    iaAddressOption.setPreferredLifetime(PREFFERRED_LT_REQ);
    iaAddressOption.setValidLifetime(VALID_LT_REQ_2);

    // IA NA
    Dhcp6IaNaOption iaNaOption = new Dhcp6IaNaOption();
    iaNaOption.setIaId(IA_ID);
    iaNaOption.setT1(T1_CLIENT);
    iaNaOption.setT2(T2_CLIENT);
    iaNaOption.setOptions(ImmutableList.of(iaAddressOption));
    options.add(iaNaOption);

    dhcp6.setOptions(options);

    Dhcp6RelayOption relayOption = new Dhcp6RelayOption();
    relayOption.setPayload(dhcp6);

    UDP udp = new UDP();
    udp.setSourcePort(UDP.DHCP_V6_CLIENT_PORT);
    udp.setDestinationPort(UDP.DHCP_V6_SERVER_PORT);
    udp.setPayload(dhcp6);
    udp.setChecksum((short) 0xffc1);

    IPv6 ipv6 = new IPv6();
    ipv6.setHopLimit((byte) 1);
    ipv6.setSourceAddress(CLIENT_LL.toOctets());
    ipv6.setDestinationAddress(DHCP6_BRC.toOctets());
    ipv6.setNextHeader(IPv6.PROTOCOL_UDP);
    ipv6.setTrafficClass((byte) 0);
    ipv6.setFlowLabel(0x000322ad);
    ipv6.setPayload(udp);

    Ethernet eth = new Ethernet();
    eth.setDestinationMACAddress(IPV6_MCAST);
    eth.setSourceMACAddress(CLIENT_MAC);
    eth.setEtherType(Ethernet.TYPE_IPV6);
    eth.setPayload(ipv6);

    assertArrayEquals(Resources.toByteArray(Dhcp6RelayTest.class.getResource(REQUEST)),
                      eth.serialize());
}
 
Example 18
Source File: Dhcp6Test.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Test serialize advertise message.
 *
 * @throws Exception exception while serialize the DHCPv6 payload
 */
@Test
public void serializeAdvertise() throws Exception {
    DHCP6 dhcp6 = new DHCP6();
    dhcp6.setMsgType(DHCP6.MsgType.ADVERTISE.value());
    dhcp6.setTransactionId(XID_1);
    List<Dhcp6Option> options = Lists.newArrayList();

    // IA address
    Dhcp6IaAddressOption iaAddressOption = new Dhcp6IaAddressOption();
    iaAddressOption.setIp6Address(IA_ADDRESS);
    iaAddressOption.setPreferredLifetime(PREFFERRED_LT_SERVER);
    iaAddressOption.setValidLifetime(VALID_LT_SERVER);

    // IA NA
    Dhcp6IaNaOption iaNaOption = new Dhcp6IaNaOption();
    iaNaOption.setIaId(IA_ID);
    iaNaOption.setT1(T1_SERVER);
    iaNaOption.setT2(T2_SERVER);
    iaNaOption.setOptions(ImmutableList.of(iaAddressOption));
    options.add(iaNaOption);

    // Client ID
    Dhcp6Duid duid = new Dhcp6Duid();
    duid.setDuidType(Dhcp6Duid.DuidType.DUID_LLT);
    duid.setHardwareType((short) 1);
    duid.setDuidTime(CLIENT_DUID_TIME);
    duid.setLinkLayerAddress(CLIENT_MAC.toBytes());
    Dhcp6ClientIdOption clientIdOption = new Dhcp6ClientIdOption();
    clientIdOption.setDuid(duid);
    options.add(clientIdOption);

    // Server ID
    Dhcp6Option option = new Dhcp6Option();
    option.setCode(DHCP6.OptionCode.SERVERID.value());
    option.setLength((short) 14);
    Dhcp6Duid serverDuid = new Dhcp6Duid();
    serverDuid.setDuidType(Dhcp6Duid.DuidType.DUID_LLT);
    serverDuid.setLinkLayerAddress(SERVER_MAC.toBytes());
    serverDuid.setHardwareType((short) 1);
    serverDuid.setDuidTime(0x211e5340);
    option.setData(serverDuid.serialize());
    options.add(option);

    dhcp6.setOptions(options);

    Dhcp6RelayOption relayOption = new Dhcp6RelayOption();
    relayOption.setPayload(dhcp6);

    UDP udp = new UDP();
    udp.setSourcePort(UDP.DHCP_V6_SERVER_PORT);
    udp.setDestinationPort(UDP.DHCP_V6_CLIENT_PORT);
    udp.setPayload(dhcp6);
    udp.setChecksum((short) 0xcb5a);

    IPv6 ipv6 = new IPv6();
    ipv6.setHopLimit((byte) 64);
    ipv6.setSourceAddress(UPSTREAM_LL.toOctets());
    ipv6.setDestinationAddress(CLIENT_LL.toOctets());
    ipv6.setNextHeader(IPv6.PROTOCOL_UDP);
    ipv6.setTrafficClass((byte) 0);
    ipv6.setFlowLabel(0x000d935f);
    ipv6.setPayload(udp);

    Ethernet eth = new Ethernet();
    eth.setDestinationMACAddress(CLIENT_MAC);
    eth.setSourceMACAddress(UPSTREAM_MAC);
    eth.setEtherType(Ethernet.TYPE_IPV6);
    eth.setPayload(ipv6);

    assertArrayEquals(Resources.toByteArray(Dhcp6RelayTest.class.getResource(ADVERTISE)),
                      eth.serialize());
}
 
Example 19
Source File: Dhcp6RelayTest.java    From onos with Apache License 2.0 4 votes vote down vote up
@Test
public void serializeReply() throws Exception {
    DHCP6 relayMsg = new DHCP6();
    relayMsg.setMsgType(DHCP6.MsgType.RELAY_REPL.value());
    relayMsg.setHopCount((byte) HOP_COUNT);
    relayMsg.setLinkAddress(LINK_ADDRESS.toOctets());
    relayMsg.setPeerAddress(PEER_ADDRESS.toOctets());

    DHCP6 relaiedDhcp6 = new DHCP6();
    relaiedDhcp6.setMsgType(DHCP6.MsgType.REPLY.value());
    relaiedDhcp6.setTransactionId(XID_2);
    List<Dhcp6Option> options = Lists.newArrayList();

    // IA address
    Dhcp6IaAddressOption iaAddressOption = new Dhcp6IaAddressOption();
    iaAddressOption.setIp6Address(IA_ADDRESS);
    iaAddressOption.setPreferredLifetime(PREFFERRED_LT_SERVER);
    iaAddressOption.setValidLifetime(VALID_LT_SERVER);

    // IA NA
    Dhcp6IaNaOption iaNaOption = new Dhcp6IaNaOption();
    iaNaOption.setIaId(IA_ID);
    iaNaOption.setT1(T1_SERVER);
    iaNaOption.setT2(T2_SERVER);
    iaNaOption.setOptions(ImmutableList.of(iaAddressOption));
    options.add(iaNaOption);

    // Client ID
    Dhcp6Duid duid = new Dhcp6Duid();
    duid.setDuidType(Dhcp6Duid.DuidType.DUID_LLT);
    duid.setHardwareType((short) 1);
    duid.setDuidTime(CLIENT_DUID_TIME);
    duid.setLinkLayerAddress(CLIENT_MAC.toBytes());
    Dhcp6ClientIdOption clientIdOption = new Dhcp6ClientIdOption();
    clientIdOption.setDuid(duid);
    options.add(clientIdOption);

    // Server ID
    Dhcp6Option option = new Dhcp6Option();
    option.setCode(DHCP6.OptionCode.SERVERID.value());
    option.setLength((short) 14);
    Dhcp6Duid serverDuid = new Dhcp6Duid();
    serverDuid.setDuidType(Dhcp6Duid.DuidType.DUID_LLT);
    serverDuid.setLinkLayerAddress(SERVER_MAC.toBytes());
    serverDuid.setHardwareType((short) 1);
    serverDuid.setDuidTime(0x211e5340);
    option.setData(serverDuid.serialize());
    options.add(option);

    relaiedDhcp6.setOptions(options);

    Dhcp6RelayOption relayOption = new Dhcp6RelayOption();
    relayOption.setPayload(relaiedDhcp6);

    relayMsg.setOptions(ImmutableList.of(relayOption));

    UDP udp = new UDP();
    udp.setSourcePort(UDP.DHCP_V6_SERVER_PORT);
    udp.setDestinationPort(UDP.DHCP_V6_SERVER_PORT);
    udp.setPayload(relayMsg);
    udp.setChecksum((short) 0x019d);

    IPv6 ipv6 = new IPv6();
    ipv6.setHopLimit((byte) 64);
    ipv6.setSourceAddress(SERVER_LL.toOctets());
    ipv6.setDestinationAddress(DOWNSTREAM_LL.toOctets());
    ipv6.setNextHeader(IPv6.PROTOCOL_UDP);
    ipv6.setTrafficClass((byte) 0);
    ipv6.setFlowLabel(0x000c72ef);
    ipv6.setPayload(udp);

    Ethernet eth = new Ethernet();
    eth.setDestinationMACAddress(DOWNSTREAM_MAC);
    eth.setSourceMACAddress(SERVER_MAC);
    eth.setEtherType(Ethernet.TYPE_IPV6);
    eth.setPayload(ipv6);

    assertArrayEquals(Resources.toByteArray(Dhcp6RelayTest.class.getResource(REPLY)),
                      eth.serialize());
}
 
Example 20
Source File: Dhcp6RelayTest.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Test serialize relay message with solicit message.
 *
 * @throws Exception exception while serialize the DHCPv6 payload
 */
@Test
public void serializeSolicit() throws Exception {
    DHCP6 relayMsg = new DHCP6();
    relayMsg.setMsgType(DHCP6.MsgType.RELAY_FORW.value());
    relayMsg.setHopCount((byte) HOP_COUNT);
    relayMsg.setLinkAddress(LINK_ADDRESS.toOctets());
    relayMsg.setPeerAddress(PEER_ADDRESS.toOctets());

    DHCP6 relaiedDhcp6 = new DHCP6();
    relaiedDhcp6.setMsgType(DHCP6.MsgType.SOLICIT.value());
    relaiedDhcp6.setTransactionId(XID_1);
    List<Dhcp6Option> options = Lists.newArrayList();

    // Client ID
    Dhcp6Duid duid = new Dhcp6Duid();
    duid.setDuidType(Dhcp6Duid.DuidType.DUID_LLT);
    duid.setHardwareType((short) 1);
    duid.setDuidTime(CLIENT_DUID_TIME);
    duid.setLinkLayerAddress(CLIENT_MAC.toBytes());
    Dhcp6ClientIdOption clientIdOption = new Dhcp6ClientIdOption();
    clientIdOption.setDuid(duid);
    options.add(clientIdOption);

    // Option request
    Dhcp6Option option = new Dhcp6Option();
    option.setCode(DHCP6.OptionCode.ORO.value());
    option.setLength((short) 8);
    option.setData(new byte[]{0, 23, 0, 24, 0, 39, 0, 31});
    options.add(option);

    // Elapsed Time
    option = new Dhcp6Option();
    option.setCode(DHCP6.OptionCode.ELAPSED_TIME.value());
    option.setLength((short) 2);
    option.setData(new byte[]{0, 0});
    options.add(option);

    // IA NA
    Dhcp6IaNaOption iaNaOption = new Dhcp6IaNaOption();
    iaNaOption.setIaId(IA_ID);
    iaNaOption.setT1(T1_CLIENT);
    iaNaOption.setT2(T2_CLIENT);
    Dhcp6IaAddressOption iaAddressOption = new Dhcp6IaAddressOption();
    iaAddressOption.setIp6Address(IA_ADDRESS);
    iaAddressOption.setPreferredLifetime(PREFFERRED_LT_REQ);
    iaAddressOption.setValidLifetime(VALID_LT_REQ);
    iaNaOption.setOptions(ImmutableList.of(iaAddressOption));
    options.add(iaNaOption);
    relaiedDhcp6.setOptions(options);

    Dhcp6RelayOption relayOption = new Dhcp6RelayOption();
    relayOption.setPayload(relaiedDhcp6);

    Dhcp6Option subscriberId = new Dhcp6Option();
    subscriberId.setCode(DHCP6.OptionCode.SUBSCRIBER_ID.value());
    subscriberId.setLength((short) 10);
    subscriberId.setData(SERVER_IP.toString().getBytes(US_ASCII));

    relayMsg.setOptions(ImmutableList.of(subscriberId, relayOption));

    UDP udp = new UDP();
    udp.setSourcePort(UDP.DHCP_V6_SERVER_PORT);
    udp.setDestinationPort(UDP.DHCP_V6_SERVER_PORT);
    udp.setPayload(relayMsg);
    udp.setChecksum((short) 0x9a99);

    IPv6 ipv6 = new IPv6();
    ipv6.setHopLimit((byte) 32);
    ipv6.setSourceAddress(DOWNSTREAM_LL.toOctets());
    ipv6.setDestinationAddress(DHCP6_BRC.toOctets());
    ipv6.setNextHeader(IPv6.PROTOCOL_UDP);
    ipv6.setTrafficClass((byte) 0);
    ipv6.setFlowLabel(0x000cbf64);
    ipv6.setPayload(udp);

    Ethernet eth = new Ethernet();
    eth.setDestinationMACAddress(IPV6_MCAST);
    eth.setSourceMACAddress(DOWNSTREAM_MAC);
    eth.setEtherType(Ethernet.TYPE_IPV6);
    eth.setPayload(ipv6);

    assertArrayEquals(Resources.toByteArray(Dhcp6RelayTest.class.getResource(SOLICIT)),
                      eth.serialize());
}