Search in sources :

Example 6 with DHCP

use of org.onlab.packet.DHCP in project onos by opennetworkinglab.

the class DhcpTest method longOptionTest.

/**
 * Test option with option length > 128.
 */
@Test
public void longOptionTest() throws Exception {
    byte[] data = Resources.toByteArray(Dhcp6RelayTest.class.getResource(LONG_OPT));
    DHCP dhcp = DHCP.deserializer().deserialize(data, 0, data.length);
    assertEquals(2, dhcp.getOptions().size());
    DhcpOption hostnameOption = dhcp.getOption(DHCP.DHCPOptionCode.OptionCode_HostName);
    DhcpOption endOption = dhcp.getOption(DHCP.DHCPOptionCode.OptionCode_END);
    assertNotNull(hostnameOption);
    assertNotNull(endOption);
    // Host name contains 200 "A"
    StringBuilder hostnameBuilder = new StringBuilder();
    IntStream.range(0, 200).forEach(i -> hostnameBuilder.append("A"));
    String hostname = hostnameBuilder.toString();
    assertEquals((byte) 200, hostnameOption.getLength());
    assertArrayEquals(hostname.getBytes(Charsets.US_ASCII), hostnameOption.getData());
}
Also used : DHCP(org.onlab.packet.DHCP) Test(org.junit.Test)

Example 7 with DHCP

use of org.onlab.packet.DHCP in project onos by opennetworkinglab.

the class DhcpTest method testDeserializeRequest.

/**
 * Tests deserialize discover packet.
 */
@Test
public void testDeserializeRequest() throws Exception {
    byte[] data = Resources.toByteArray(Dhcp6RelayTest.class.getResource(REQUEST));
    Ethernet eth = Ethernet.deserializer().deserialize(data, 0, data.length);
    DHCP dhcp = (DHCP) eth.getPayload().getPayload().getPayload();
    assertEquals(DHCP.OPCODE_REQUEST, dhcp.getOpCode());
    assertEquals(HW_TYPE, dhcp.getHardwareType());
    assertEquals(HW_ADDR_LEN, dhcp.getHardwareAddressLength());
    assertEquals(HOPS, dhcp.getHops());
    assertEquals(XID, dhcp.getTransactionId());
    assertEquals(SECS, dhcp.getSeconds());
    assertEquals(FLAGS, dhcp.getFlags());
    assertEquals(NO_IP, dhcp.getClientIPAddress());
    assertEquals(NO_IP, dhcp.getYourIPAddress());
    assertEquals(NO_IP, dhcp.getServerIPAddress());
    assertEquals(GW_IP.toInt(), dhcp.getGatewayIPAddress());
    assertTrue(Arrays.equals(CLIENT_HW_ADDR.toBytes(), dhcp.getClientHardwareAddress()));
    assertEquals(EMPTY, dhcp.getServerName());
    assertEquals(EMPTY, dhcp.getBootFileName());
    assertEquals(7, dhcp.getOptions().size());
    DhcpOption option = dhcp.getOptions().get(0);
    assertEquals(DHCP.DHCPOptionCode.OptionCode_MessageType.getValue(), option.code);
    assertEquals(1, option.length);
    assertEquals(DHCP.MsgType.DHCPREQUEST.getValue(), (int) option.getData()[0]);
    option = dhcp.getOptions().get(1);
    assertEquals(DHCP.DHCPOptionCode.OptionCode_DHCPServerIp.getValue(), option.code);
    assertEquals(4, option.length);
    assertArrayEquals(SERVER_IP.toOctets(), option.getData());
    option = dhcp.getOptions().get(2);
    assertEquals(DHCP.DHCPOptionCode.OptionCode_RequestedIP.getValue(), option.code);
    assertEquals(4, option.length);
    assertArrayEquals(CLIENT_IP.toOctets(), option.getData());
    option = dhcp.getOptions().get(3);
    assertEquals(DHCP.DHCPOptionCode.OptionCode_HostName.getValue(), option.code);
    assertEquals(9, option.length);
    assertArrayEquals(HOSTNAME.getBytes(Charsets.US_ASCII), option.getData());
    option = dhcp.getOptions().get(4);
    assertEquals(DHCP.DHCPOptionCode.OptionCode_RequestedParameters.getValue(), option.code);
    assertEquals(13, option.length);
    assertArrayEquals(new byte[] { 1, 28, 2, 3, 15, 6, 119, 12, 44, 47, 26, 121, 42 }, option.getData());
    option = dhcp.getOptions().get(5);
    assertTrue(option instanceof DhcpRelayAgentOption);
    DhcpRelayAgentOption relayAgentOption = (DhcpRelayAgentOption) option;
    assertEquals(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue(), relayAgentOption.code);
    assertEquals(12, relayAgentOption.length);
    DhcpOption subOption = relayAgentOption.getSubOption(DhcpRelayAgentOption.RelayAgentInfoOptions.CIRCUIT_ID.getValue());
    assertEquals(10, subOption.getLength());
    assertArrayEquals(CIRCUIT_ID.getBytes(Charsets.US_ASCII), subOption.getData());
    option = dhcp.getOptions().get(6);
    assertEquals(DHCP.DHCPOptionCode.OptionCode_END.getValue(), option.code);
    assertEquals(0, option.length);
}
Also used : Ethernet(org.onlab.packet.Ethernet) DHCP(org.onlab.packet.DHCP) Test(org.junit.Test)

Example 8 with DHCP

use of org.onlab.packet.DHCP in project onos by opennetworkinglab.

the class OpenstackSwitchingDhcpHandlerTest method validatePacket.

/**
 * Validates the contents of the packet sent by the OpenstackSwitchingDhcpHandler.
 *
 * @param packet Ethernet packet received
 */
private void validatePacket(Ethernet packet) {
    DHCP dhcpPacket = (DHCP) packet.getPayload().getPayload().getPayload();
    assertEquals(MacAddress.valueOf(dhcpPacket.getClientHardwareAddress()), CLIENT_HOST.mac());
    assertEquals(Ip4Address.valueOf(dhcpPacket.getYourIPAddress()), Ip4Address.valueOf(EXPECTED_IP));
    assertEquals(dhcpPacket.getTransactionId(), TRANSACTION_ID);
    if (testMsgType == DHCP.MsgType.DHCPDISCOVER) {
        assertEquals(dhcpPacket.getPacketType(), DHCP.MsgType.DHCPOFFER);
    }
    if (testMsgType == DHCP.MsgType.DHCPREQUEST) {
        assertEquals(dhcpPacket.getPacketType(), DHCP.MsgType.DHCPACK);
    }
}
Also used : DHCP(org.onlab.packet.DHCP)

Example 9 with DHCP

use of org.onlab.packet.DHCP in project onos by opennetworkinglab.

the class Dhcp4HandlerImpl method processDhcpPacket.

@Override
public void processDhcpPacket(PacketContext context, BasePacket payload) {
    checkNotNull(payload, "DHCP payload can't be null");
    checkState(payload instanceof DHCP, "Payload is not a DHCP");
    DHCP dhcpPayload = (DHCP) payload;
    if (!configured()) {
        log.warn("Missing default DHCP relay server config. Abort packet processing");
        return;
    }
    ConnectPoint inPort = context.inPacket().receivedFrom();
    checkNotNull(dhcpPayload, "Can't find DHCP payload");
    Ethernet packet = context.inPacket().parsed();
    DHCP.MsgType incomingPacketType = dhcpPayload.getOptions().stream().filter(dhcpOption -> dhcpOption.getCode() == OptionCode_MessageType.getValue()).map(DhcpOption::getData).map(data -> DHCP.MsgType.getType(data[0])).findFirst().orElse(null);
    checkNotNull(incomingPacketType, "Can't get message type from DHCP payload {}", dhcpPayload);
    Set<Interface> receivingInterfaces = interfaceService.getInterfacesByPort(inPort);
    // ignore the packets if dhcp client interface is not configured on onos.
    if (receivingInterfaces.isEmpty()) {
        log.warn("Virtual interface is not configured on {}", inPort);
        return;
    }
    switch(incomingPacketType) {
        case DHCPDISCOVER:
            // Add the gateway IP as virtual interface IP for server to understand
            // the lease to be assigned and forward the packet to dhcp server.
            List<InternalPacket> ethernetClientPacket = processDhcpPacketFromClient(context, packet, receivingInterfaces);
            for (InternalPacket internalPacket : ethernetClientPacket) {
                log.debug("DHCPDISCOVER from {} Forward to server", inPort);
                writeRequestDhcpRecord(inPort, packet, dhcpPayload);
                forwardPacket(internalPacket);
            }
            break;
        case DHCPOFFER:
            // reply to dhcp client.
            InternalPacket ethernetPacketOffer = processDhcpPacketFromServer(context, packet);
            if (ethernetPacketOffer != null) {
                writeResponseDhcpRecord(ethernetPacketOffer.getPacket(), dhcpPayload);
                sendResponseToClient(ethernetPacketOffer, dhcpPayload);
            }
            break;
        case DHCPREQUEST:
            // add the gateway ip as virtual interface ip for server to understand
            // the lease to be assigned and forward the packet to dhcp server.
            List<InternalPacket> ethernetPacketRequest = processDhcpPacketFromClient(context, packet, receivingInterfaces);
            for (InternalPacket internalPacket : ethernetPacketRequest) {
                log.debug("DHCPDISCOVER from {} Forward to server", inPort);
                writeRequestDhcpRecord(inPort, packet, dhcpPayload);
                forwardPacket(internalPacket);
            }
            break;
        case DHCPDECLINE:
            break;
        case DHCPACK:
            // reply to dhcp client.
            InternalPacket ethernetPacketAck = processDhcpPacketFromServer(context, packet);
            if (ethernetPacketAck != null) {
                writeResponseDhcpRecord(ethernetPacketAck.getPacket(), dhcpPayload);
                handleDhcpAck(ethernetPacketAck.getPacket(), dhcpPayload);
                sendResponseToClient(ethernetPacketAck, dhcpPayload);
            }
            break;
        case DHCPNAK:
            break;
        case DHCPRELEASE:
            // TODO: release the ip address from client
            break;
        case DHCPINFORM:
            break;
        case DHCPFORCERENEW:
            break;
        case DHCPLEASEQUERY:
            handleLeaseQueryMsg(context, packet, dhcpPayload);
            break;
        case DHCPLEASEACTIVE:
            handleLeaseQueryActivateMsg(packet, dhcpPayload);
            break;
        case DHCPLEASEUNASSIGNED:
        case DHCPLEASEUNKNOWN:
            handleLeaseQueryUnknown(packet, dhcpPayload);
            break;
        default:
            break;
    }
}
Also used : Ethernet(org.onlab.packet.Ethernet) ConnectPoint(org.onosproject.net.ConnectPoint) DHCP(org.onlab.packet.DHCP) Interface(org.onosproject.net.intf.Interface)

Example 10 with DHCP

use of org.onlab.packet.DHCP in project onos by opennetworkinglab.

the class Dhcp4HandlerImpl method setDhcpServerConfigs.

public void setDhcpServerConfigs(Collection<DhcpServerConfig> configs, List<DhcpServerInfo> serverInfoList) {
    if (configs.size() == 0) {
        // no config to update
        return;
    }
    Boolean isConfigValid = false;
    for (DhcpServerConfig serverConfig : configs) {
        if (serverConfig.getDhcpServerIp4().isPresent()) {
            isConfigValid = true;
            break;
        }
    }
    if (!isConfigValid) {
        log.warn("No IP V4 server address found.");
        // No IP V6 address found
        return;
    }
    // if (!serverInfoList.isEmpty()) {
    for (DhcpServerInfo oldServerInfo : serverInfoList) {
        log.info("In for (DhcpServerInfo oldServerInfo : serverInfoList) {");
        // remove old server info
        // DhcpServerInfo oldServerInfo = serverInfoList.remove(0);
        // stop monitoring gateway or server
        oldServerInfo.getDhcpGatewayIp4().ifPresent(gatewayIp -> {
            hostService.stopMonitoringIp(gatewayIp);
        });
        oldServerInfo.getDhcpServerIp4().ifPresent(serverIp -> {
            hostService.stopMonitoringIp(serverIp);
            cancelDhcpPacket(serverIp);
        });
    }
    // Create new server info according to the config
    serverInfoList.clear();
    for (DhcpServerConfig serverConfig : configs) {
        log.debug("Create new server info according to the config");
        DhcpServerInfo newServerInfo = new DhcpServerInfo(serverConfig, DhcpServerInfo.Version.DHCP_V4);
        checkState(newServerInfo.getDhcpServerConnectPoint().isPresent(), "Connect point not exists");
        checkState(newServerInfo.getDhcpServerIp4().isPresent(), "IP of DHCP server not exists");
        log.debug("DHCP server connect point: {}", newServerInfo.getDhcpServerConnectPoint().orElse(null));
        log.debug("DHCP server IP: {}", newServerInfo.getDhcpServerIp4().orElse(null));
        Ip4Address serverIp = newServerInfo.getDhcpServerIp4().get();
        Ip4Address ipToProbe;
        if (newServerInfo.getDhcpGatewayIp4().isPresent()) {
            ipToProbe = newServerInfo.getDhcpGatewayIp4().get();
        } else {
            ipToProbe = newServerInfo.getDhcpServerIp4().orElse(null);
        }
        log.info("Probe_IP {}", ipToProbe);
        String hostToProbe = newServerInfo.getDhcpGatewayIp4().map(ip -> "gateway").orElse("server");
        log.debug("Probing to resolve {} IP {}", hostToProbe, ipToProbe);
        hostService.startMonitoringIp(ipToProbe);
        Set<Host> hosts = hostService.getHostsByIp(ipToProbe);
        if (!hosts.isEmpty()) {
            Host host = hosts.iterator().next();
            newServerInfo.setDhcpConnectVlan(host.vlan());
            newServerInfo.setDhcpConnectMac(host.mac());
        }
        // Add new server info
        synchronized (this) {
            // serverInfoList.clear();
            serverInfoList.add(newServerInfo);
        }
        requestDhcpPacket(serverIp);
    }
}
Also used : DeviceService(org.onosproject.net.device.DeviceService) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment) ConnectPoint(org.onosproject.net.ConnectPoint) ObjectiveError(org.onosproject.net.flowobjective.ObjectiveError) OptionCode_END(org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_END) IgnoreDhcpConfig(org.onosproject.dhcprelay.config.IgnoreDhcpConfig) Port(org.onosproject.net.Port) ApplicationId(org.onosproject.core.ApplicationId) DhcpRelayStore(org.onosproject.dhcprelay.store.DhcpRelayStore) Ip4Address(org.onlab.packet.Ip4Address) Deactivate(org.osgi.service.component.annotations.Deactivate) OptionCode_MessageType(org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_MessageType) Set(java.util.Set) MacAddress.valueOf(org.onlab.packet.MacAddress.valueOf) PacketService(org.onosproject.net.packet.PacketService) DhcpServerConfig(org.onosproject.dhcprelay.config.DhcpServerConfig) DeviceId(org.onosproject.net.DeviceId) LEARN_ROUTE_FROM_LEASE_QUERY_DEFAULT(org.onosproject.dhcprelay.OsgiPropertyConstants.LEARN_ROUTE_FROM_LEASE_QUERY_DEFAULT) Dictionary(java.util.Dictionary) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Pipeliner(org.onosproject.net.behaviour.Pipeliner) REMOVE(org.onosproject.net.flowobjective.Objective.Operation.REMOVE) HostProviderRegistry(org.onosproject.net.host.HostProviderRegistry) Tools(org.onlab.util.Tools) Host(org.onosproject.net.Host) RouteStore(org.onosproject.routeservice.RouteStore) ComponentContext(org.osgi.service.component.ComponentContext) LEARN_ROUTE_FROM_LEASE_QUERY(org.onosproject.dhcprelay.OsgiPropertyConstants.LEARN_ROUTE_FROM_LEASE_QUERY) HostListener(org.onosproject.net.host.HostListener) InterfaceService(org.onosproject.net.intf.InterfaceService) HostService(org.onosproject.net.host.HostService) Multimaps(com.google.common.collect.Multimaps) ArrayList(java.util.ArrayList) Component(org.osgi.service.component.annotations.Component) Lists(com.google.common.collect.Lists) TrafficSelector(org.onosproject.net.flow.TrafficSelector) CIRCUIT_ID(org.onlab.packet.dhcp.DhcpRelayAgentOption.RelayAgentInfoOptions.CIRCUIT_ID) OptionCode_CircuitID(org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_CircuitID) CircuitId(org.onlab.packet.dhcp.CircuitId) DhcpRelayAgentOption(org.onlab.packet.dhcp.DhcpRelayAgentOption) DefaultTrafficSelector(org.onosproject.net.flow.DefaultTrafficSelector) TpPort(org.onlab.packet.TpPort) ComponentConfigService(org.onosproject.cfg.ComponentConfigService) BasePacket(org.onlab.packet.BasePacket) Executor(java.util.concurrent.Executor) HostProvider(org.onosproject.net.host.HostProvider) VlanId(org.onlab.packet.VlanId) ProviderId(org.onosproject.net.provider.ProviderId) IPv4(org.onlab.packet.IPv4) DhcpRecord(org.onosproject.dhcprelay.store.DhcpRecord) Objective(org.onosproject.net.flowobjective.Objective) MacAddress(org.onlab.packet.MacAddress) DHCP(org.onlab.packet.DHCP) DefaultOutboundPacket(org.onosproject.net.packet.DefaultOutboundPacket) Route(org.onosproject.routeservice.Route) HostLocation(org.onosproject.net.HostLocation) Interface(org.onosproject.net.intf.Interface) CoreService(org.onosproject.core.CoreService) LoggerFactory(org.slf4j.LoggerFactory) Tools.groupedThreads(org.onlab.util.Tools.groupedThreads) ForwardingObjective(org.onosproject.net.flowobjective.ForwardingObjective) ByteBuffer(java.nio.ByteBuffer) Ethernet(org.onlab.packet.Ethernet) HostProviderService(org.onosproject.net.host.HostProviderService) HashMultimap(com.google.common.collect.HashMultimap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DhcpServerInfo(org.onosproject.dhcprelay.api.DhcpServerInfo) ImmutableSet(com.google.common.collect.ImmutableSet) Device(org.onosproject.net.Device) Collection(java.util.Collection) Executors.newSingleThreadExecutor(java.util.concurrent.Executors.newSingleThreadExecutor) DefaultForwardingObjective(org.onosproject.net.flowobjective.DefaultForwardingObjective) InterfaceIpAddress(org.onosproject.net.host.InterfaceIpAddress) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) ObjectiveContext(org.onosproject.net.flowobjective.ObjectiveContext) PacketContext(org.onosproject.net.packet.PacketContext) Optional(java.util.Optional) HostDescription(org.onosproject.net.host.HostDescription) ADD(org.onosproject.net.flowobjective.Objective.Operation.ADD) Multimap(com.google.common.collect.Multimap) FlowObjectiveService(org.onosproject.net.flowobjective.FlowObjectiveService) DefaultHostDescription(org.onosproject.net.host.DefaultHostDescription) OutboundPacket(org.onosproject.net.packet.OutboundPacket) HostEvent(org.onosproject.net.host.HostEvent) Activate(org.osgi.service.component.annotations.Activate) HostId(org.onosproject.net.HostId) IpAddress(org.onlab.packet.IpAddress) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) Logger(org.slf4j.Logger) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) UDP(org.onlab.packet.UDP) DhcpHandler(org.onosproject.dhcprelay.api.DhcpHandler) DhcpOption(org.onlab.packet.dhcp.DhcpOption) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) Modified(org.osgi.service.component.annotations.Modified) PacketPriority(org.onosproject.net.packet.PacketPriority) Reference(org.osgi.service.component.annotations.Reference) Collections(java.util.Collections) DhcpServerConfig(org.onosproject.dhcprelay.config.DhcpServerConfig) Ip4Address(org.onlab.packet.Ip4Address) Host(org.onosproject.net.Host) DhcpServerInfo(org.onosproject.dhcprelay.api.DhcpServerInfo)

Aggregations

DHCP (org.onlab.packet.DHCP)33 Ethernet (org.onlab.packet.Ethernet)27 UDP (org.onlab.packet.UDP)23 IPv4 (org.onlab.packet.IPv4)22 DhcpOption (org.onlab.packet.dhcp.DhcpOption)12 ConnectPoint (org.onosproject.net.ConnectPoint)11 ArrayList (java.util.ArrayList)9 MacAddress (org.onlab.packet.MacAddress)9 Interface (org.onosproject.net.intf.Interface)9 ByteBuffer (java.nio.ByteBuffer)8 Ip4Address (org.onlab.packet.Ip4Address)8 VlanId (org.onlab.packet.VlanId)8 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)7 Lists (com.google.common.collect.Lists)7 Collection (java.util.Collection)7 List (java.util.List)7 Optional (java.util.Optional)7 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)7 Collectors (java.util.stream.Collectors)7 IpAddress (org.onlab.packet.IpAddress)7