use of org.onlab.packet.Ip4Address in project onos by opennetworkinglab.
the class Dhcp4HandlerImpl method handleLeaseQueryActivateMsg.
private void handleLeaseQueryActivateMsg(Ethernet packet, DHCP dhcpPayload) {
log.debug("LQ: Got DHCPLEASEACTIVE packet!");
if (learnRouteFromLeasequery) {
// TODO: release the ip address from client
MacAddress clientMacAddress = MacAddress.valueOf(dhcpPayload.getClientHardwareAddress());
VlanId vlanId = VlanId.vlanId(packet.getVlanID());
HostId hostId = HostId.hostId(clientMacAddress, vlanId);
DhcpRecord record = dhcpRelayStore.getDhcpRecord(hostId).orElse(null);
if (record == null) {
log.warn("Can't find record for host {} when processing DHCPLEASEACTIVE", hostId);
return;
}
// need to update routes
log.debug("Lease Query for Client results in DHCPLEASEACTIVE - route needs to be modified");
// get current route
// find the ip of that client with the DhcpRelay store
Ip4Address clientIP = record.ip4Address().orElse(null);
log.debug("LQ: IP of host is " + clientIP.getIp4Address());
MacAddress nextHopMac = record.nextHop().orElse(null);
log.debug("LQ: MAC of resulting *OLD* NH for that host is " + nextHopMac.toString());
// find the new NH by looking at the src MAC of the dhcp request
// from the LQ store
MacAddress newNextHopMac = record.nextHopTemp().orElse(null);
log.debug("LQ: MAC of resulting *NEW* NH for that host is " + newNextHopMac.toString());
log.debug("LQ: updating dhcp relay record with new NH");
record.nextHop(newNextHopMac);
// find the next hop IP from its mac
HostId gwHostId = HostId.hostId(newNextHopMac, vlanId);
Host gwHost = hostService.getHost(gwHostId);
if (gwHost == null) {
log.warn("Can't find gateway for new NH host " + gwHostId);
return;
}
Ip4Address nextHopIp = gwHost.ipAddresses().stream().filter(IpAddress::isIp4).map(IpAddress::getIp4Address).findFirst().orElse(null);
if (nextHopIp == null) {
log.warn("Can't find IP address of gateway " + gwHost);
return;
}
log.debug("LQ: *NEW* NH IP for host is " + nextHopIp.getIp4Address());
Route route = new Route(Route.Source.DHCP, clientIP.toIpPrefix(), nextHopIp);
routeStore.updateRoute(route);
}
// and forward to client
InternalPacket ethernetPacket = processLeaseQueryFromServer(packet);
if (ethernetPacket != null) {
sendResponseToClient(ethernetPacket, dhcpPayload);
}
}
use of org.onlab.packet.Ip4Address in project onos by opennetworkinglab.
the class Dhcp4HandlerImpl method processLeaseQueryFromAgent.
/**
* Do a basic routing for a packet from client (used for LQ processing).
*
* @param context the packet context
* @param ethernetPacket the ethernet payload to process
* @return processed packet
*/
private List<InternalPacket> processLeaseQueryFromAgent(PacketContext context, Ethernet ethernetPacket) {
ConnectPoint receivedFrom = context.inPacket().receivedFrom();
DeviceId receivedFromDevice = receivedFrom.deviceId();
// get dhcp header.
Ethernet etherReply = (Ethernet) ethernetPacket.clone();
IPv4 ipv4Packet = (IPv4) etherReply.getPayload();
UDP udpPacket = (UDP) ipv4Packet.getPayload();
DHCP dhcpPacket = (DHCP) udpPacket.getPayload();
Ip4Address relayAgentIp;
Ip4Address clientInterfaceIp = interfaceService.getInterfacesByPort(context.inPacket().receivedFrom()).stream().map(Interface::ipAddressesList).flatMap(Collection::stream).map(InterfaceIpAddress::ipAddress).filter(IpAddress::isIp4).map(IpAddress::getIp4Address).findFirst().orElse(null);
if (clientInterfaceIp == null) {
log.warn("Can't find interface IP for client interface for port {}", context.inPacket().receivedFrom());
return null;
}
boolean isDirectlyConnected = directlyConnected(dhcpPacket);
boolean directConnFlag = directlyConnected(dhcpPacket);
// Multi DHCP Start
List<InternalPacket> internalPackets = new ArrayList<>();
List<DhcpServerInfo> serverInfoList = findValidServerInfo(directConnFlag);
List<DhcpServerInfo> copyServerInfoList = new ArrayList<>(serverInfoList);
for (DhcpServerInfo serverInfo : copyServerInfoList) {
// get dhcp header.
etherReply = (Ethernet) ethernetPacket.clone();
ipv4Packet = (IPv4) etherReply.getPayload();
udpPacket = (UDP) ipv4Packet.getPayload();
dhcpPacket = (DHCP) udpPacket.getPayload();
if (!checkDhcpServerConnPt(directConnFlag, serverInfo)) {
log.warn("Can't get server connect point, ignore");
continue;
}
DhcpServerInfo newServerInfo = getHostInfoForServerInfo(serverInfo, serverInfoList);
if (newServerInfo == null) {
log.warn("Can't get server interface with host info resolved, ignore");
continue;
}
Interface serverInterface = getServerInterface(newServerInfo);
if (serverInterface == null) {
log.warn("Can't get server interface, ignore");
continue;
}
Ip4Address ipFacingServer = getFirstIpFromInterface(serverInterface);
MacAddress macFacingServer = serverInterface.mac();
if (ipFacingServer == null || macFacingServer == null) {
log.warn("No IP address for server Interface {}", serverInterface);
continue;
}
etherReply.setSourceMACAddress(macFacingServer);
etherReply.setDestinationMACAddress(newServerInfo.getDhcpConnectMac().get());
etherReply.setVlanID(newServerInfo.getDhcpConnectVlan().get().toShort());
ipv4Packet.setSourceAddress(ipFacingServer.toInt());
ipv4Packet.setDestinationAddress(newServerInfo.getDhcpServerIp4().get().toInt());
if (isDirectlyConnected) {
// set default info and replace with indirect if available later on.
if (newServerInfo.getDhcpConnectMac().isPresent()) {
etherReply.setDestinationMACAddress(newServerInfo.getDhcpConnectMac().get());
}
if (newServerInfo.getDhcpConnectVlan().isPresent()) {
etherReply.setVlanID(serverInfo.getDhcpConnectVlan().get().toShort());
}
if (learnRouteFromLeasequery) {
relayAgentIp = newServerInfo.getRelayAgentIp4(receivedFromDevice).orElse(null);
// Sets relay agent IP
int effectiveRelayAgentIp = relayAgentIp != null ? relayAgentIp.toInt() : clientInterfaceIp.toInt();
dhcpPacket.setGatewayIPAddress(effectiveRelayAgentIp);
}
} else {
if (!newServerInfo.getDhcpServerIp4().isPresent()) {
// do nothing
} else if (!newServerInfo.getDhcpConnectMac().isPresent()) {
continue;
} else if (learnRouteFromLeasequery) {
relayAgentIp = newServerInfo.getRelayAgentIp4(receivedFromDevice).orElse(null);
// Sets relay agent IP
int effectiveRelayAgentIp = relayAgentIp != null ? relayAgentIp.toInt() : clientInterfaceIp.toInt();
dhcpPacket.setGatewayIPAddress(effectiveRelayAgentIp);
log.debug("Relay Agent IP {}", relayAgentIp);
}
log.trace("Indirect");
}
// Remove broadcast flag
dhcpPacket.setFlags((short) 0);
udpPacket.setPayload(dhcpPacket);
// As a DHCP relay, the source port should be server port( instead
// of client port.
udpPacket.setSourcePort(UDP.DHCP_SERVER_PORT);
udpPacket.setDestinationPort(UDP.DHCP_SERVER_PORT);
ipv4Packet.setPayload(udpPacket);
ipv4Packet.setTtl((byte) 64);
etherReply.setPayload(ipv4Packet);
udpPacket.resetChecksum();
// //return etherReply;
InternalPacket internalPacket = InternalPacket.internalPacket(etherReply, newServerInfo.getDhcpServerConnectPoint().get());
internalPackets.add(internalPacket);
}
log.debug("num of processLeaseQueryFromAgent packets to send is: {}", internalPackets.size());
return internalPackets;
}
use of org.onlab.packet.Ip4Address in project onos by opennetworkinglab.
the class Dhcp4HandlerImpl method hostRemoved.
private void hostRemoved(Host host, List<DhcpServerInfo> serverInfoList) {
serverInfoList.stream().forEach(serverInfo -> {
Ip4Address targetIp = serverInfo.getDhcpGatewayIp4().orElse(null);
Ip4Address serverIp = serverInfo.getDhcpServerIp4().orElse(null);
if (targetIp == null) {
targetIp = serverIp;
}
if (targetIp != null) {
if (host.ipAddresses().contains(targetIp)) {
serverInfo.setDhcpConnectVlan(null);
serverInfo.setDhcpConnectMac(null);
cancelDhcpPacket(serverIp);
}
}
});
}
use of org.onlab.packet.Ip4Address in project onos by opennetworkinglab.
the class DhcpRelayManager method getDhcpServerMacAddress.
@Override
public Optional<MacAddress> getDhcpServerMacAddress() {
// TODO: depreated it
DefaultDhcpRelayConfig config = cfgService.getConfig(appId, DefaultDhcpRelayConfig.class);
DhcpServerConfig serverConfig = config.dhcpServerConfigs().get(0);
Ip4Address serverip = serverConfig.getDhcpServerIp4().get();
return hostService.getHostsByIp(serverip).stream().map(Host::mac).findFirst();
}
use of org.onlab.packet.Ip4Address in project onos by opennetworkinglab.
the class DistributedDhcpStore method releaseIP.
@Override
public Ip4Address releaseIP(HostId hostId) {
if (allocationMap.containsKey(hostId)) {
// If the IP has been assigned with Option_RangeNotEnforced,
// we do not release the IP address nor remove the host from HostService.
// Therefore, if the IP is assigned statically, the IP needs to be released statically.
Versioned<IpAssignment> assignmentVersioned = allocationMap.get(hostId);
if (Versioned.valueOrNull(assignmentVersioned) != null && assignmentVersioned.value().assignmentStatus().equals(Option_RangeNotEnforced)) {
return null;
}
IpAssignment newAssignment = IpAssignment.builder(allocationMap.get(hostId).value()).assignmentStatus(IpAssignment.AssignmentStatus.Option_Expired).build();
Ip4Address freeIP = newAssignment.ipAddress();
allocationMap.put(hostId, newAssignment);
if (ipWithinRange(freeIP)) {
freeIPPool.add(freeIP);
}
return freeIP;
}
return null;
}
Aggregations