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

The following examples show how to use org.onlab.packet.Ethernet#setPayload() . 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: Dhcp4HandlerImpl.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Removes DHCP relay agent information option (option 82) from DHCP payload.
 * Also reset giaddr to 0
 *
 * @param ethPacket the Ethernet packet to be processed
 * @return Ethernet packet processed
 */
private Ethernet removeRelayAgentOption(Ethernet ethPacket) {
    Ethernet ethernet = (Ethernet) ethPacket.duplicate();
    IPv4 ipv4 = (IPv4) ethernet.getPayload();
    UDP udp = (UDP) ipv4.getPayload();
    DHCP dhcpPayload = (DHCP) udp.getPayload();

    // removes relay agent information option
    List<DhcpOption> options = dhcpPayload.getOptions();
    options = options.stream()
            .filter(option -> option.getCode() != OptionCode_CircuitID.getValue())
            .collect(Collectors.toList());
    dhcpPayload.setOptions(options);
    dhcpPayload.setGatewayIPAddress(0);

    udp.setPayload(dhcpPayload);
    ipv4.setPayload(udp);
    ethernet.setPayload(ipv4);
    return ethernet;
}
 
Example 2
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 3
Source File: NeighbourTestUtils.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an ARP request for the given target IP.
 *
 * @param targetIp IP address
 * @return ARP request packet
 */
public static Ethernet createArpRequest(IpAddress targetIp) {
    Ethernet eth = new Ethernet();
    eth.setDestinationMACAddress(MAC1);
    eth.setSourceMACAddress(MAC2);
    eth.setEtherType(Ethernet.TYPE_ARP);

    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(MAC2.toBytes());
    arp.setTargetHardwareAddress(MacAddress.ZERO.toBytes());

    arp.setTargetProtocolAddress(targetIp.toOctets());
    arp.setSenderProtocolAddress(IP2.toOctets());

    eth.setPayload(arp);
    return eth;
}
 
Example 4
Source File: NetworkConfigLinksProviderTest.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public InboundPacket inPacket() {
    ONOSLLDP lldp = ONOSLLDP.onosSecureLLDP(src.deviceId().toString(),
                                            new ChassisId(),
                                            (int) src.port().toLong(), "", "test-secret");

    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");

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

}
 
Example 5
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 6
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 7
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 8
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 9
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 NDP replies.
 *
 * @param deviceId the target device
 * @param destIpAddress the destination ip address
 */
public void forwardPackets(DeviceId deviceId, Ip6Address destIpAddress) {
    if (ipPacketQueue.get(destIpAddress) == null) {
        return;
    }
    for (IP ipPacket : ipPacketQueue.get(destIpAddress)) {
        if (ipPacket.getVersion() == ((byte) 6)) {
            IPv6 ipv6Packet = (IPv6) ipPacket;
            Ip6Address destAddress = Ip6Address.valueOf(ipv6Packet.getDestinationAddress());
            if (config.inSameSubnet(deviceId, destAddress)) {
                ipv6Packet.setHopLimit((byte) (ipv6Packet.getHopLimit() - 1));
                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_IPV6);
                    eth.setPayload(ipv6Packet);
                    forwardToHost(deviceId, eth, dest);
                    ipPacketQueue.get(destIpAddress).remove(ipPacket);
                }
                ipPacketQueue.get(destIpAddress).remove(ipPacket);
            }
        }
        ipPacketQueue.get(destIpAddress).remove(ipPacket);
    }
}
 
Example 10
Source File: PimInterface.java    From onos with Apache License 2.0 5 votes vote down vote up
private void sendJoinPrune(McastRoute route, RouteData data, boolean join) {
    PIMJoinPrune jp = new PIMJoinPrune();

    jp.addJoinPrune(route.source().toIpPrefix(), route.group().toIpPrefix(), join);
    jp.setHoldTime(join ? (short) Math.floor(JOIN_PERIOD * HOLD_TIME_MULTIPLIER) : 0);
    jp.setUpstreamAddr(new PIMAddrUnicast(data.ipAddress.toString()));

    PIM pim = new PIM();
    pim.setPIMType(PIM.TYPE_JOIN_PRUNE_REQUEST);
    pim.setPayload(jp);

    IPv4 ipv4 = new IPv4();
    ipv4.setDestinationAddress(PIM.PIM_ADDRESS.getIp4Address().toInt());
    ipv4.setSourceAddress(getIpAddress().getIp4Address().toInt());
    ipv4.setProtocol(IPv4.PROTOCOL_PIM);
    ipv4.setTtl((byte) 1);
    ipv4.setDiffServ((byte) 0xc0);
    ipv4.setPayload(pim);

    Ethernet eth = new Ethernet();
    eth.setSourceMACAddress(onosInterface.mac());
    eth.setDestinationMACAddress(MacAddress.valueOf("01:00:5E:00:00:0d"));
    eth.setEtherType(Ethernet.TYPE_IPV4);
    eth.setPayload(ipv4);

    TrafficTreatment treatment = DefaultTrafficTreatment.builder()
            .setOutput(onosInterface.connectPoint().port())
            .build();

    packetService.emit(new DefaultOutboundPacket(onosInterface.connectPoint().deviceId(),
            treatment, ByteBuffer.wrap(eth.serialize())));

    data.timestamp = System.currentTimeMillis();
}
 
Example 11
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 12
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 13
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 14
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 15
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 16
Source File: OpenstackRoutingSnatIcmpHandlerTest.java    From onos with Apache License 2.0 4 votes vote down vote up
private Ethernet constructIcmpRequestPacket(IpAddress srcIp,
                                            MacAddress srcMac,
                                            IpAddress dstIp,
                                            MacAddress dstMac, byte icmpType) {
    try {
        ICMPEcho icmpEcho = new ICMPEcho();
        icmpEcho.setIdentifier((short) 0)
                .setSequenceNum((short) 0);
        ByteBuffer byteBufferIcmpEcho = ByteBuffer.wrap(icmpEcho.serialize());

        ICMP icmp = new ICMP();
        icmp.setIcmpType(icmpType)
                .setIcmpCode(icmpType == TYPE_ECHO_REQUEST ? CODE_ECHO_REQUEST : CODE_ECHO_REPLY)
                .setChecksum((short) 0);

        icmp.setPayload(ICMPEcho.deserializer().deserialize(byteBufferIcmpEcho.array(),
                0, ICMPEcho.ICMP_ECHO_HEADER_LENGTH));

        ByteBuffer byteBufferIcmp = ByteBuffer.wrap(icmp.serialize());

        IPv4 iPacket = new IPv4();
        iPacket.setDestinationAddress(dstIp.toString());
        iPacket.setSourceAddress(srcIp.toString());
        iPacket.setTtl((byte) 64);
        iPacket.setChecksum((short) 0);
        iPacket.setDiffServ((byte) 0);
        iPacket.setProtocol(IPv4.PROTOCOL_ICMP);

        iPacket.setPayload(ICMP.deserializer().deserialize(byteBufferIcmp.array(), 0, 8));

        Ethernet ethPacket = new Ethernet();

        ethPacket.setEtherType(TYPE_IPV4);
        ethPacket.setSourceMACAddress(srcMac);
        ethPacket.setDestinationMACAddress(dstMac);
        ethPacket.setPayload(iPacket);

        return ethPacket;
    } catch (DeserializationException e) {
        return null;
    }
}
 
Example 17
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 18
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 19
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 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());
}