Search in sources :

Example 46 with IPv4

use of org.onlab.packet.IPv4 in project trellis-control by opennetworkinglab.

the class IpHandler method forwardPackets.

/**
 * 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);
            }
        }
    }
}
Also used : IP(org.onlab.packet.IP) IPv4(org.onlab.packet.IPv4) Ethernet(org.onlab.packet.Ethernet) Ip4Address(org.onlab.packet.Ip4Address) Host(org.onosproject.net.Host) DeviceConfigNotFoundException(org.onosproject.segmentrouting.config.DeviceConfigNotFoundException)

Example 47 with IPv4

use of org.onlab.packet.IPv4 in project dhcpl2relay by opencord.

the class DhcpL2RelayTest method compareServerPackets.

public void compareServerPackets(Ethernet sent, Ethernet relayed, VlanId expectedVlan, short expectedPcp) {
    try {
        // modify sent packet to create expected packet
        sent.setDestinationMACAddress(CLIENT_MAC);
        sent.setQinQVID(NOT_PROVIDED);
        sent.setQinQPriorityCode((byte) NOT_PROVIDED);
        sent.setVlanID(expectedVlan.toShort());
        sent.setPriorityCode((byte) expectedPcp);
        DHCP d = ((DHCP) ((UDP) ((IPv4) sent.getPayload()).getPayload()).getPayload());
        List<DhcpOption> newOptions = d.getOptions().stream().filter(option -> option.getCode() != DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue()).collect(Collectors.toList());
        d.setOptions(newOptions);
        final ByteBuffer byteBuffer = ByteBuffer.wrap(sent.serialize());
        Ethernet expectedPacket = Ethernet.deserializer().deserialize(byteBuffer.array(), 0, byteBuffer.array().length);
        assertEquals(expectedPacket, relayed);
    } catch (Exception e) {
        log.error(e.getMessage());
        fail();
    }
}
Also used : UDP(org.onlab.packet.UDP) DhcpL2RelayStoreDelegate(org.opencord.dhcpl2relay.DhcpL2RelayStoreDelegate) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ClusterServiceAdapter(org.onosproject.cluster.ClusterServiceAdapter) DhcpL2RelayEvent(org.opencord.dhcpl2relay.DhcpL2RelayEvent) ByteBuffer(java.nio.ByteBuffer) ConnectPoint(org.onosproject.net.ConnectPoint) Ethernet(org.onlab.packet.Ethernet) LeadershipServiceAdapter(org.onosproject.cluster.LeadershipServiceAdapter) ClusterCommunicationServiceAdapter(org.onosproject.store.cluster.messaging.ClusterCommunicationServiceAdapter) DhcpAllocationInfo(org.opencord.dhcpl2relay.DhcpAllocationInfo) Lists(com.google.common.collect.Lists) DeserializationException(org.onlab.packet.DeserializationException) Map(java.util.Map) After(org.junit.After) TestUtils(org.onlab.junit.TestUtils) Assert.fail(org.junit.Assert.fail) EasyMock.createMock(org.easymock.EasyMock.createMock) FlowObjectiveServiceAdapter(org.onosproject.net.flowobjective.FlowObjectiveServiceAdapter) Before(org.junit.Before) ComponentConfigService(org.onosproject.cfg.ComponentConfigService) TestStorageService(org.onosproject.store.service.TestStorageService) DhcpOption82Data(org.opencord.dhcpl2relay.impl.packet.DhcpOption82Data) Logger(org.slf4j.Logger) VlanId(org.onlab.packet.VlanId) Test(org.junit.Test) Collectors(java.util.stream.Collectors) UDP(org.onlab.packet.UDP) DhcpOption(org.onlab.packet.dhcp.DhcpOption) List(java.util.List) IPv4(org.onlab.packet.IPv4) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) MacAddress(org.onlab.packet.MacAddress) DHCP(org.onlab.packet.DHCP) Assert.assertEquals(org.junit.Assert.assertEquals) Ethernet(org.onlab.packet.Ethernet) DhcpOption(org.onlab.packet.dhcp.DhcpOption) ByteBuffer(java.nio.ByteBuffer) DHCP(org.onlab.packet.DHCP) DeserializationException(org.onlab.packet.DeserializationException)

Example 48 with IPv4

use of org.onlab.packet.IPv4 in project dhcpl2relay by opencord.

the class DhcpL2RelayTestBase method constructEthernetPacket.

private Ethernet constructEthernetPacket(MacAddress srcMac, MacAddress dstMac, String dstIp, byte dhcpReqRsp, MacAddress clientHwAddress, Ip4Address dhcpClientIpAddress, VlanId clientVlan, short clientPbit) {
    // Ethernet Frame.
    Ethernet ethPkt = new Ethernet();
    ethPkt.setSourceMACAddress(srcMac);
    ethPkt.setDestinationMACAddress(dstMac);
    ethPkt.setEtherType(Ethernet.TYPE_IPV4);
    ethPkt.setVlanID(clientVlan.toShort());
    ethPkt.setPriorityCode((byte) clientPbit);
    if (DHCP.OPCODE_REPLY == dhcpReqRsp) {
        ethPkt.setQinQPriorityCode((byte) 3);
        ethPkt.setQinQVID((short) 4);
    }
    // IP Packet
    IPv4 ipv4Reply = new IPv4();
    ipv4Reply.setSourceAddress(0);
    ipv4Reply.setDestinationAddress(dstIp);
    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(dhcpReqRsp);
    dhcpReply.setYourIPAddress(dhcpClientIpAddress.toInt());
    dhcpReply.setServerIPAddress(0);
    final byte[] serverNameBytes = new byte[64];
    String result = new String(serverNameBytes, StandardCharsets.US_ASCII).trim();
    dhcpReply.setServerName(result);
    final byte[] bootFileBytes = new byte[128];
    String result1 = new String(bootFileBytes, StandardCharsets.US_ASCII).trim();
    dhcpReply.setBootFileName(result1);
    dhcpReply.setTransactionId(TRANSACTION_ID);
    dhcpReply.setClientHardwareAddress(clientHwAddress.toBytes());
    dhcpReply.setHardwareType(DHCP.HWTYPE_ETHERNET);
    dhcpReply.setHardwareAddressLength((byte) 6);
    udpReply.setPayload(dhcpReply);
    ipv4Reply.setPayload(udpReply);
    ethPkt.setPayload(ipv4Reply);
    return ethPkt;
}
Also used : UDP(org.onlab.packet.UDP) Ethernet(org.onlab.packet.Ethernet) IPv4(org.onlab.packet.IPv4) DHCP(org.onlab.packet.DHCP)

Example 49 with IPv4

use of org.onlab.packet.IPv4 in project dhcpl2relay by opencord.

the class DhcpL2RelayTestBase method constructDhcpRequestPacket.

/**
 * Constructs DHCP Request Packet.
 *
 * @return Ethernet packet
 */
Ethernet constructDhcpRequestPacket(MacAddress clientMac) {
    Ethernet pkt = construcEthernetPacket(clientMac, MacAddress.BROADCAST, "255.255.255.255", DHCP.OPCODE_REQUEST, clientMac, Ip4Address.valueOf("0.0.0.0"));
    IPv4 ipv4Packet = (IPv4) pkt.getPayload();
    UDP udpPacket = (UDP) ipv4Packet.getPayload();
    DHCP dhcpPacket = (DHCP) udpPacket.getPayload();
    dhcpPacket.setOptions(constructDhcpOptions(DHCP.MsgType.DHCPREQUEST));
    return pkt;
}
Also used : UDP(org.onlab.packet.UDP) Ethernet(org.onlab.packet.Ethernet) IPv4(org.onlab.packet.IPv4) DHCP(org.onlab.packet.DHCP)

Example 50 with IPv4

use of org.onlab.packet.IPv4 in project dhcpl2relay by opencord.

the class DhcpL2RelayTestBase method constructDhcpDiscoverPacket.

/**
 * Constructs DHCP Discover Packet with client VLAN information.
 *
 * @return Ethernet packet
 */
Ethernet constructDhcpDiscoverPacket(MacAddress clientMac, VlanId clientVlan, short clientPbit) {
    Ethernet pkt = constructEthernetPacket(clientMac, MacAddress.BROADCAST, "255.255.255.255", DHCP.OPCODE_REQUEST, clientMac, Ip4Address.valueOf("0.0.0.0"), clientVlan, clientPbit);
    IPv4 ipv4Packet = (IPv4) pkt.getPayload();
    UDP udpPacket = (UDP) ipv4Packet.getPayload();
    DHCP dhcpPacket = (DHCP) udpPacket.getPayload();
    dhcpPacket.setOptions(constructDhcpOptions(DHCP.MsgType.DHCPDISCOVER));
    return pkt;
}
Also used : UDP(org.onlab.packet.UDP) Ethernet(org.onlab.packet.Ethernet) IPv4(org.onlab.packet.IPv4) DHCP(org.onlab.packet.DHCP)

Aggregations

IPv4 (org.onlab.packet.IPv4)54 Ethernet (org.onlab.packet.Ethernet)43 UDP (org.onlab.packet.UDP)24 DHCP (org.onlab.packet.DHCP)18 ICMP (org.onlab.packet.ICMP)17 IpAddress (org.onlab.packet.IpAddress)15 DefaultTrafficTreatment (org.onosproject.net.flow.DefaultTrafficTreatment)14 TrafficTreatment (org.onosproject.net.flow.TrafficTreatment)13 Test (org.junit.Test)12 Ip4Address (org.onlab.packet.Ip4Address)10 MacAddress (org.onlab.packet.MacAddress)10 ConnectPoint (org.onosproject.net.ConnectPoint)10 DefaultTrafficSelector (org.onosproject.net.flow.DefaultTrafficSelector)10 TrafficSelector (org.onosproject.net.flow.TrafficSelector)10 ByteBuffer (java.nio.ByteBuffer)8 Optional (java.util.Optional)8 Interface (org.onosproject.net.intf.Interface)8 DefaultOutboundPacket (org.onosproject.net.packet.DefaultOutboundPacket)8 DhcpOption (org.onlab.packet.dhcp.DhcpOption)7 ApplicationId (org.onosproject.core.ApplicationId)7