samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 1 | /* |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 2 | * Copyright 2015 Open Networking Laboratory |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.dhcp.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
daniel | cd9deed | 2015-10-30 17:16:16 +0900 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 26 | import org.jboss.netty.util.Timeout; |
| 27 | import org.jboss.netty.util.TimerTask; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 28 | import org.onlab.packet.ARP; |
| 29 | import org.onlab.packet.DHCP; |
| 30 | import org.onlab.packet.DHCPOption; |
samanwita pal | 8969cbe | 2015-09-04 13:31:30 -0700 | [diff] [blame] | 31 | import org.onlab.packet.DHCPPacketType; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 32 | import org.onlab.packet.Ethernet; |
| 33 | import org.onlab.packet.IPv4; |
| 34 | import org.onlab.packet.Ip4Address; |
| 35 | import org.onlab.packet.IpAddress; |
| 36 | import org.onlab.packet.MacAddress; |
| 37 | import org.onlab.packet.TpPort; |
| 38 | import org.onlab.packet.UDP; |
| 39 | import org.onlab.packet.VlanId; |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 40 | import org.onlab.util.Timer; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 41 | import org.onosproject.core.ApplicationId; |
| 42 | import org.onosproject.core.CoreService; |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 43 | import org.onosproject.dhcp.DhcpService; |
| 44 | import org.onosproject.dhcp.DhcpStore; |
| 45 | import org.onosproject.dhcp.IpAssignment; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 46 | import org.onosproject.net.ConnectPoint; |
| 47 | import org.onosproject.net.Host; |
| 48 | import org.onosproject.net.HostId; |
| 49 | import org.onosproject.net.HostLocation; |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 50 | import org.onosproject.net.config.ConfigFactory; |
| 51 | import org.onosproject.net.config.NetworkConfigEvent; |
| 52 | import org.onosproject.net.config.NetworkConfigListener; |
| 53 | import org.onosproject.net.config.NetworkConfigRegistry; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 54 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 55 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 56 | import org.onosproject.net.flow.TrafficSelector; |
| 57 | import org.onosproject.net.flow.TrafficTreatment; |
| 58 | import org.onosproject.net.host.DefaultHostDescription; |
| 59 | import org.onosproject.net.host.HostProvider; |
| 60 | import org.onosproject.net.host.HostProviderRegistry; |
| 61 | import org.onosproject.net.host.HostProviderService; |
| 62 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 63 | import org.onosproject.net.packet.PacketContext; |
| 64 | import org.onosproject.net.packet.PacketPriority; |
| 65 | import org.onosproject.net.packet.PacketProcessor; |
| 66 | import org.onosproject.net.packet.PacketService; |
| 67 | import org.onosproject.net.provider.AbstractProvider; |
| 68 | import org.onosproject.net.provider.ProviderId; |
| 69 | import org.slf4j.Logger; |
| 70 | import org.slf4j.LoggerFactory; |
| 71 | |
| 72 | import java.nio.ByteBuffer; |
| 73 | import java.util.ArrayList; |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 74 | import java.util.Date; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 75 | import java.util.HashSet; |
| 76 | import java.util.List; |
| 77 | import java.util.Map; |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 78 | import java.util.Objects; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 79 | import java.util.Set; |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 80 | import java.util.concurrent.TimeUnit; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 81 | import static org.onlab.packet.MacAddress.valueOf; |
| 82 | import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
| 83 | |
| 84 | /** |
| 85 | * Skeletal ONOS DHCP Server application. |
| 86 | */ |
| 87 | @Component(immediate = true) |
| 88 | @Service |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 89 | public class DhcpManager implements DhcpService { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 90 | |
| 91 | private static final ProviderId PID = new ProviderId("of", "org.onosproject.dhcp", true); |
| 92 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 93 | |
Thomas Vachuska | 0009044 | 2015-09-11 18:08:04 -0700 | [diff] [blame] | 94 | private final InternalConfigListener cfgListener = new InternalConfigListener(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 95 | |
| 96 | private final Set<ConfigFactory> factories = ImmutableSet.of( |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 97 | new ConfigFactory<ApplicationId, DhcpConfig>(APP_SUBJECT_FACTORY, |
| 98 | DhcpConfig.class, |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 99 | "dhcp") { |
| 100 | @Override |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 101 | public DhcpConfig createConfig() { |
| 102 | return new DhcpConfig(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 103 | } |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 104 | } |
| 105 | ); |
| 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 107 | protected NetworkConfigRegistry cfgService; |
| 108 | |
| 109 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 110 | protected PacketService packetService; |
| 111 | |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 112 | private DhcpPacketProcessor processor = new DhcpPacketProcessor(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 113 | |
| 114 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 115 | protected CoreService coreService; |
| 116 | |
| 117 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 118 | protected DhcpStore dhcpStore; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 119 | |
| 120 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 121 | protected HostProviderRegistry hostProviderRegistry; |
| 122 | |
| 123 | protected HostProviderService hostProviderService; |
| 124 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 125 | private final HostProvider hostProvider = new InternalHostProvider(); |
| 126 | |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 127 | private ApplicationId appId; |
| 128 | |
| 129 | // Hardcoded values are default values. |
| 130 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 131 | private static Ip4Address myIP = Ip4Address.valueOf("10.0.0.2"); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 132 | |
| 133 | private static MacAddress myMAC = valueOf("4f:4f:4f:4f:4f:4f"); |
| 134 | |
| 135 | /** |
| 136 | * leaseTime - 10 mins or 600s. |
| 137 | * renewalTime - 5 mins or 300s. |
| 138 | * rebindingTime - 6 mins or 360s. |
| 139 | */ |
| 140 | |
| 141 | private static int leaseTime = 600; |
| 142 | |
| 143 | private static int renewalTime = 300; |
| 144 | |
| 145 | private static int rebindingTime = 360; |
| 146 | |
| 147 | private static byte packetTTL = (byte) 127; |
| 148 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 149 | private static Ip4Address subnetMask = Ip4Address.valueOf("255.0.0.0"); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 150 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 151 | private static Ip4Address broadcastAddress = Ip4Address.valueOf("10.255.255.255"); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 152 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 153 | private static Ip4Address routerAddress = Ip4Address.valueOf("10.0.0.2"); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 154 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 155 | private static Ip4Address domainServer = Ip4Address.valueOf("10.0.0.2"); |
| 156 | |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 157 | private static final Ip4Address IP_BROADCAST = Ip4Address.valueOf("255.255.255.255"); |
| 158 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 159 | protected Timeout timeout; |
| 160 | |
| 161 | protected static int timerDelay = 2; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 162 | |
| 163 | @Activate |
| 164 | protected void activate() { |
| 165 | // start the dhcp server |
| 166 | appId = coreService.registerApplication("org.onosproject.dhcp"); |
| 167 | |
| 168 | cfgService.addListener(cfgListener); |
| 169 | factories.forEach(cfgService::registerConfigFactory); |
Thomas Vachuska | 0009044 | 2015-09-11 18:08:04 -0700 | [diff] [blame] | 170 | cfgListener.reconfigureNetwork(cfgService.getConfig(appId, DhcpConfig.class)); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 171 | hostProviderService = hostProviderRegistry.register(hostProvider); |
Thomas Vachuska | 2a645d4 | 2015-09-11 18:58:36 -0700 | [diff] [blame] | 172 | packetService.addProcessor(processor, PacketProcessor.director(0)); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 173 | requestPackets(); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 174 | timeout = Timer.getTimer().newTimeout(new PurgeListTask(), timerDelay, TimeUnit.MINUTES); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 175 | log.info("Started"); |
| 176 | } |
| 177 | |
| 178 | @Deactivate |
| 179 | protected void deactivate() { |
| 180 | cfgService.removeListener(cfgListener); |
| 181 | factories.forEach(cfgService::unregisterConfigFactory); |
| 182 | packetService.removeProcessor(processor); |
| 183 | hostProviderRegistry.unregister(hostProvider); |
| 184 | hostProviderService = null; |
| 185 | cancelPackets(); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 186 | timeout.cancel(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 187 | log.info("Stopped"); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Request packet in via PacketService. |
| 192 | */ |
| 193 | private void requestPackets() { |
| 194 | |
| 195 | TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder() |
| 196 | .matchEthType(Ethernet.TYPE_IPV4) |
| 197 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
| 198 | .matchUdpDst(TpPort.tpPort(UDP.DHCP_SERVER_PORT)) |
| 199 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_CLIENT_PORT)); |
| 200 | packetService.requestPackets(selectorServer.build(), PacketPriority.CONTROL, appId); |
| 201 | |
| 202 | selectorServer = DefaultTrafficSelector.builder() |
| 203 | .matchEthType(Ethernet.TYPE_ARP); |
| 204 | packetService.requestPackets(selectorServer.build(), PacketPriority.CONTROL, appId); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Cancel requested packets in via packet service. |
| 209 | */ |
| 210 | private void cancelPackets() { |
| 211 | TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder() |
| 212 | .matchEthType(Ethernet.TYPE_IPV4) |
| 213 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
| 214 | .matchUdpDst(TpPort.tpPort(UDP.DHCP_SERVER_PORT)) |
| 215 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_CLIENT_PORT)); |
| 216 | packetService.cancelPackets(selectorServer.build(), PacketPriority.CONTROL, appId); |
| 217 | |
| 218 | selectorServer = DefaultTrafficSelector.builder() |
| 219 | .matchEthType(Ethernet.TYPE_ARP); |
| 220 | packetService.cancelPackets(selectorServer.build(), PacketPriority.CONTROL, appId); |
| 221 | } |
| 222 | |
| 223 | @Override |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 224 | public Map<HostId, IpAssignment> listMapping() { |
samanwita pal | 0bff430 | 2015-09-15 13:37:00 -0700 | [diff] [blame] | 225 | return dhcpStore.listAssignedMapping(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public int getLeaseTime() { |
| 230 | return leaseTime; |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public int getRenewalTime() { |
| 235 | return renewalTime; |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public int getRebindingTime() { |
| 240 | return rebindingTime; |
| 241 | } |
| 242 | |
| 243 | @Override |
daniel | 877bb2f | 2015-11-12 21:33:05 +0900 | [diff] [blame] | 244 | public boolean setStaticMapping(MacAddress macID, Ip4Address ipAddress, boolean rangeNotEnforced, |
daniel | cd9deed | 2015-10-30 17:16:16 +0900 | [diff] [blame] | 245 | List<Ip4Address> addressList) { |
| 246 | log.debug("setStaticMapping is called with Mac: {}, Ip: {} addressList: {}", |
| 247 | macID.toString(), ipAddress.toString(), addressList.toString()); |
| 248 | |
daniel | 877bb2f | 2015-11-12 21:33:05 +0900 | [diff] [blame] | 249 | return dhcpStore.assignStaticIP(macID, ipAddress, rangeNotEnforced, addressList); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public boolean removeStaticMapping(MacAddress macID) { |
| 254 | return dhcpStore.removeStaticIP(macID); |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public Iterable<Ip4Address> getAvailableIPs() { |
| 259 | return dhcpStore.getAvailableIPs(); |
| 260 | } |
| 261 | |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 262 | private class DhcpPacketProcessor implements PacketProcessor { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 263 | |
| 264 | /** |
| 265 | * Builds the DHCP Reply packet. |
| 266 | * |
| 267 | * @param packet the incoming Ethernet frame |
| 268 | * @param ipOffered the IP offered by the DHCP Server |
| 269 | * @param outgoingMessageType the message type of the outgoing packet |
| 270 | * @return the Ethernet reply frame |
| 271 | */ |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 272 | private Ethernet buildReply(Ethernet packet, Ip4Address ipOffered, byte outgoingMessageType) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 273 | |
daniel | cd9deed | 2015-10-30 17:16:16 +0900 | [diff] [blame] | 274 | Ip4Address subnetMaskReply; |
| 275 | Ip4Address dhcpServerReply; |
| 276 | Ip4Address routerAddressReply; |
| 277 | Ip4Address domainServerReply; |
| 278 | IpAssignment ipAssignment; |
| 279 | |
| 280 | ipAssignment = dhcpStore.getIpAssignmentFromAllocationMap(HostId.hostId(packet.getSourceMAC())); |
| 281 | |
daniel | 877bb2f | 2015-11-12 21:33:05 +0900 | [diff] [blame] | 282 | if (ipAssignment != null && ipAssignment.rangeNotEnforced()) { |
daniel | cd9deed | 2015-10-30 17:16:16 +0900 | [diff] [blame] | 283 | subnetMaskReply = ipAssignment.subnetMask(); |
| 284 | dhcpServerReply = ipAssignment.dhcpServer(); |
| 285 | domainServerReply = ipAssignment.domainServer(); |
| 286 | routerAddressReply = ipAssignment.routerAddress(); |
| 287 | } else { |
| 288 | subnetMaskReply = subnetMask; |
| 289 | dhcpServerReply = myIP; |
| 290 | routerAddressReply = routerAddress; |
| 291 | domainServerReply = domainServer; |
| 292 | } |
| 293 | |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 294 | // Ethernet Frame. |
| 295 | Ethernet ethReply = new Ethernet(); |
| 296 | ethReply.setSourceMACAddress(myMAC); |
| 297 | ethReply.setDestinationMACAddress(packet.getSourceMAC()); |
| 298 | ethReply.setEtherType(Ethernet.TYPE_IPV4); |
| 299 | ethReply.setVlanID(packet.getVlanID()); |
| 300 | |
| 301 | // IP Packet |
| 302 | IPv4 ipv4Packet = (IPv4) packet.getPayload(); |
| 303 | IPv4 ipv4Reply = new IPv4(); |
daniel | cd9deed | 2015-10-30 17:16:16 +0900 | [diff] [blame] | 304 | ipv4Reply.setSourceAddress(dhcpServerReply.toInt()); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 305 | ipv4Reply.setDestinationAddress(ipOffered.toInt()); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 306 | ipv4Reply.setTtl(packetTTL); |
| 307 | |
| 308 | // UDP Datagram. |
| 309 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 310 | UDP udpReply = new UDP(); |
| 311 | udpReply.setSourcePort((byte) UDP.DHCP_SERVER_PORT); |
| 312 | udpReply.setDestinationPort((byte) UDP.DHCP_CLIENT_PORT); |
| 313 | |
| 314 | // DHCP Payload. |
| 315 | DHCP dhcpPacket = (DHCP) udpPacket.getPayload(); |
| 316 | DHCP dhcpReply = new DHCP(); |
| 317 | dhcpReply.setOpCode(DHCP.OPCODE_REPLY); |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 318 | dhcpReply.setFlags(dhcpPacket.getFlags()); |
| 319 | dhcpReply.setGatewayIPAddress(dhcpPacket.getGatewayIPAddress()); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 320 | dhcpReply.setClientHardwareAddress(dhcpPacket.getClientHardwareAddress()); |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 321 | dhcpReply.setTransactionId(dhcpPacket.getTransactionId()); |
| 322 | |
| 323 | if (outgoingMessageType != DHCPPacketType.DHCPNAK.getValue()) { |
| 324 | dhcpReply.setYourIPAddress(ipOffered.toInt()); |
daniel | cd9deed | 2015-10-30 17:16:16 +0900 | [diff] [blame] | 325 | dhcpReply.setServerIPAddress(dhcpServerReply.toInt()); |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 326 | if (dhcpPacket.getGatewayIPAddress() == 0) { |
| 327 | ipv4Reply.setDestinationAddress(IP_BROADCAST.toInt()); |
| 328 | } |
| 329 | } |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 330 | dhcpReply.setHardwareType(DHCP.HWTYPE_ETHERNET); |
| 331 | dhcpReply.setHardwareAddressLength((byte) 6); |
| 332 | |
| 333 | // DHCP Options. |
| 334 | DHCPOption option = new DHCPOption(); |
| 335 | List<DHCPOption> optionList = new ArrayList<>(); |
| 336 | |
| 337 | // DHCP Message Type. |
| 338 | option.setCode(DHCP.DHCPOptionCode.OptionCode_MessageType.getValue()); |
| 339 | option.setLength((byte) 1); |
| 340 | byte[] optionData = {outgoingMessageType}; |
| 341 | option.setData(optionData); |
| 342 | optionList.add(option); |
| 343 | |
| 344 | // DHCP Server Identifier. |
| 345 | option = new DHCPOption(); |
| 346 | option.setCode(DHCP.DHCPOptionCode.OptionCode_DHCPServerIp.getValue()); |
| 347 | option.setLength((byte) 4); |
daniel | cd9deed | 2015-10-30 17:16:16 +0900 | [diff] [blame] | 348 | option.setData(dhcpServerReply.toOctets()); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 349 | optionList.add(option); |
| 350 | |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 351 | if (outgoingMessageType != DHCPPacketType.DHCPNAK.getValue()) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 352 | |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 353 | // IP Address Lease Time. |
| 354 | option = new DHCPOption(); |
| 355 | option.setCode(DHCP.DHCPOptionCode.OptionCode_LeaseTime.getValue()); |
| 356 | option.setLength((byte) 4); |
| 357 | option.setData(ByteBuffer.allocate(4).putInt(leaseTime).array()); |
| 358 | optionList.add(option); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 359 | |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 360 | // IP Address Renewal Time. |
| 361 | option = new DHCPOption(); |
| 362 | option.setCode(DHCP.DHCPOptionCode.OptionCode_RenewalTime.getValue()); |
| 363 | option.setLength((byte) 4); |
| 364 | option.setData(ByteBuffer.allocate(4).putInt(renewalTime).array()); |
| 365 | optionList.add(option); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 366 | |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 367 | // IP Address Rebinding Time. |
| 368 | option = new DHCPOption(); |
| 369 | option.setCode(DHCP.DHCPOptionCode.OPtionCode_RebindingTime.getValue()); |
| 370 | option.setLength((byte) 4); |
| 371 | option.setData(ByteBuffer.allocate(4).putInt(rebindingTime).array()); |
| 372 | optionList.add(option); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 373 | |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 374 | // Subnet Mask. |
| 375 | option = new DHCPOption(); |
| 376 | option.setCode(DHCP.DHCPOptionCode.OptionCode_SubnetMask.getValue()); |
| 377 | option.setLength((byte) 4); |
daniel | cd9deed | 2015-10-30 17:16:16 +0900 | [diff] [blame] | 378 | option.setData(subnetMaskReply.toOctets()); |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 379 | optionList.add(option); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 380 | |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 381 | // Broadcast Address. |
| 382 | option = new DHCPOption(); |
| 383 | option.setCode(DHCP.DHCPOptionCode.OptionCode_BroadcastAddress.getValue()); |
| 384 | option.setLength((byte) 4); |
| 385 | option.setData(broadcastAddress.toOctets()); |
| 386 | optionList.add(option); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 387 | |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 388 | // Router Address. |
| 389 | option = new DHCPOption(); |
| 390 | option.setCode(DHCP.DHCPOptionCode.OptionCode_RouterAddress.getValue()); |
| 391 | option.setLength((byte) 4); |
daniel | cd9deed | 2015-10-30 17:16:16 +0900 | [diff] [blame] | 392 | option.setData(routerAddressReply.toOctets()); |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 393 | optionList.add(option); |
| 394 | |
| 395 | // DNS Server Address. |
| 396 | option = new DHCPOption(); |
| 397 | option.setCode(DHCP.DHCPOptionCode.OptionCode_DomainServer.getValue()); |
| 398 | option.setLength((byte) 4); |
daniel | cd9deed | 2015-10-30 17:16:16 +0900 | [diff] [blame] | 399 | option.setData(domainServerReply.toOctets()); |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 400 | optionList.add(option); |
| 401 | } |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 402 | |
| 403 | // End Option. |
| 404 | option = new DHCPOption(); |
| 405 | option.setCode(DHCP.DHCPOptionCode.OptionCode_END.getValue()); |
| 406 | option.setLength((byte) 1); |
| 407 | optionList.add(option); |
| 408 | |
| 409 | dhcpReply.setOptions(optionList); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 410 | udpReply.setPayload(dhcpReply); |
| 411 | ipv4Reply.setPayload(udpReply); |
| 412 | ethReply.setPayload(ipv4Reply); |
| 413 | |
| 414 | return ethReply; |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * Sends the Ethernet reply frame via the Packet Service. |
| 419 | * |
| 420 | * @param context the context of the incoming frame |
| 421 | * @param reply the Ethernet reply frame |
| 422 | */ |
| 423 | private void sendReply(PacketContext context, Ethernet reply) { |
| 424 | if (reply != null) { |
| 425 | TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder(); |
| 426 | ConnectPoint sourcePoint = context.inPacket().receivedFrom(); |
| 427 | builder.setOutput(sourcePoint.port()); |
Thomas Vachuska | 0009044 | 2015-09-11 18:08:04 -0700 | [diff] [blame] | 428 | context.block(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 429 | packetService.emit(new DefaultOutboundPacket(sourcePoint.deviceId(), |
samanwita pal | 8969cbe | 2015-09-04 13:31:30 -0700 | [diff] [blame] | 430 | builder.build(), ByteBuffer.wrap(reply.serialize()))); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Processes the DHCP Payload and initiates a reply to the client. |
| 436 | * |
| 437 | * @param context context of the incoming message |
| 438 | * @param dhcpPayload the extracted DHCP payload |
| 439 | */ |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 440 | private void processDhcpPacket(PacketContext context, DHCP dhcpPayload) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 441 | Ethernet packet = context.inPacket().parsed(); |
| 442 | boolean flagIfRequestedIP = false; |
| 443 | boolean flagIfServerIP = false; |
| 444 | Ip4Address requestedIP = Ip4Address.valueOf("0.0.0.0"); |
| 445 | Ip4Address serverIP = Ip4Address.valueOf("0.0.0.0"); |
| 446 | |
| 447 | if (dhcpPayload != null) { |
| 448 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 449 | DHCPPacketType incomingPacketType = DHCPPacketType.getType(0); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 450 | for (DHCPOption option : dhcpPayload.getOptions()) { |
| 451 | if (option.getCode() == DHCP.DHCPOptionCode.OptionCode_MessageType.getValue()) { |
| 452 | byte[] data = option.getData(); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 453 | incomingPacketType = DHCPPacketType.getType(data[0]); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 454 | } |
| 455 | if (option.getCode() == DHCP.DHCPOptionCode.OptionCode_RequestedIP.getValue()) { |
| 456 | byte[] data = option.getData(); |
| 457 | requestedIP = Ip4Address.valueOf(data); |
| 458 | flagIfRequestedIP = true; |
| 459 | } |
| 460 | if (option.getCode() == DHCP.DHCPOptionCode.OptionCode_DHCPServerIp.getValue()) { |
| 461 | byte[] data = option.getData(); |
| 462 | serverIP = Ip4Address.valueOf(data); |
| 463 | flagIfServerIP = true; |
| 464 | } |
| 465 | } |
samanwita pal | 8969cbe | 2015-09-04 13:31:30 -0700 | [diff] [blame] | 466 | DHCPPacketType outgoingPacketType; |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 467 | MacAddress clientMac = new MacAddress(dhcpPayload.getClientHardwareAddress()); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 468 | VlanId vlanId = VlanId.vlanId(packet.getVlanID()); |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 469 | HostId hostId = HostId.hostId(clientMac, vlanId); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 470 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 471 | if (incomingPacketType.getValue() == DHCPPacketType.DHCPDISCOVER.getValue()) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 472 | |
samanwita pal | 8969cbe | 2015-09-04 13:31:30 -0700 | [diff] [blame] | 473 | outgoingPacketType = DHCPPacketType.DHCPOFFER; |
daniel | cd9deed | 2015-10-30 17:16:16 +0900 | [diff] [blame] | 474 | Ip4Address ipOffered = null; |
| 475 | ipOffered = dhcpStore.suggestIP(hostId, requestedIP); |
| 476 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 477 | if (ipOffered != null) { |
| 478 | Ethernet ethReply = buildReply(packet, ipOffered, |
| 479 | (byte) outgoingPacketType.getValue()); |
| 480 | sendReply(context, ethReply); |
| 481 | } |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 482 | } else if (incomingPacketType.getValue() == DHCPPacketType.DHCPREQUEST.getValue()) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 483 | |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 484 | if (flagIfServerIP && flagIfRequestedIP) { |
| 485 | // SELECTING state |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 486 | |
daniel | 877bb2f | 2015-11-12 21:33:05 +0900 | [diff] [blame] | 487 | |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 488 | if (dhcpStore.getIpAssignmentFromAllocationMap(HostId.hostId(clientMac)) |
daniel | 877bb2f | 2015-11-12 21:33:05 +0900 | [diff] [blame] | 489 | .rangeNotEnforced()) { |
daniel | cd9deed | 2015-10-30 17:16:16 +0900 | [diff] [blame] | 490 | outgoingPacketType = DHCPPacketType.DHCPACK; |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 491 | Ethernet ethReply = buildReply(packet, requestedIP, (byte) outgoingPacketType.getValue()); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 492 | sendReply(context, ethReply); |
daniel | cd9deed | 2015-10-30 17:16:16 +0900 | [diff] [blame] | 493 | } else { |
| 494 | if (myIP.equals(serverIP)) { |
| 495 | if (dhcpStore.assignIP(hostId, requestedIP, leaseTime, false, Lists.newArrayList())) { |
| 496 | outgoingPacketType = DHCPPacketType.DHCPACK; |
| 497 | discoverHost(context, requestedIP); |
| 498 | } else { |
| 499 | outgoingPacketType = DHCPPacketType.DHCPNAK; |
| 500 | } |
| 501 | Ethernet ethReply = buildReply(packet, requestedIP, |
| 502 | (byte) outgoingPacketType.getValue()); |
| 503 | sendReply(context, ethReply); |
| 504 | } |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 505 | } |
| 506 | } else if (flagIfRequestedIP) { |
| 507 | // INIT-REBOOT state |
daniel | cd9deed | 2015-10-30 17:16:16 +0900 | [diff] [blame] | 508 | if (dhcpStore.assignIP(hostId, requestedIP, leaseTime, false, Lists.newArrayList())) { |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 509 | outgoingPacketType = DHCPPacketType.DHCPACK; |
| 510 | Ethernet ethReply = buildReply(packet, requestedIP, (byte) outgoingPacketType.getValue()); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 511 | sendReply(context, ethReply); |
| 512 | discoverHost(context, requestedIP); |
| 513 | } |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 514 | |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 515 | } else { |
| 516 | // RENEWING and REBINDING state |
| 517 | int ciaadr = dhcpPayload.getClientIPAddress(); |
| 518 | if (ciaadr != 0) { |
| 519 | Ip4Address clientIaddr = Ip4Address.valueOf(ciaadr); |
daniel | cd9deed | 2015-10-30 17:16:16 +0900 | [diff] [blame] | 520 | if (dhcpStore.assignIP(hostId, clientIaddr, leaseTime, false, Lists.newArrayList())) { |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 521 | outgoingPacketType = DHCPPacketType.DHCPACK; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 522 | discoverHost(context, clientIaddr); |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 523 | } else if (packet.getEtherType() == Ethernet.TYPE_IPV4 && |
| 524 | ((IPv4) packet.getPayload()).getDestinationAddress() == myIP.toInt()) { |
| 525 | outgoingPacketType = DHCPPacketType.DHCPNAK; |
| 526 | } else { |
| 527 | return; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 528 | } |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 529 | Ethernet ethReply = buildReply(packet, clientIaddr, (byte) outgoingPacketType.getValue()); |
| 530 | sendReply(context, ethReply); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 531 | } |
| 532 | } |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 533 | } else if (incomingPacketType.getValue() == DHCPPacketType.DHCPRELEASE.getValue()) { |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 534 | Ip4Address ip4Address = dhcpStore.releaseIP(hostId); |
| 535 | if (ip4Address != null) { |
| 536 | hostProviderService.removeIpFromHost(hostId, ip4Address); |
| 537 | } |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | /** |
| 543 | * Processes the ARP Payload and initiates a reply to the client. |
| 544 | * |
| 545 | * @param context context of the incoming message |
| 546 | * @param packet the ethernet payload |
| 547 | */ |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 548 | private void processArpPacket(PacketContext context, Ethernet packet) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 549 | |
| 550 | ARP arpPacket = (ARP) packet.getPayload(); |
| 551 | |
| 552 | ARP arpReply = (ARP) arpPacket.clone(); |
| 553 | arpReply.setOpCode(ARP.OP_REPLY); |
| 554 | |
| 555 | arpReply.setTargetProtocolAddress(arpPacket.getSenderProtocolAddress()); |
| 556 | arpReply.setTargetHardwareAddress(arpPacket.getSenderHardwareAddress()); |
| 557 | arpReply.setSenderProtocolAddress(arpPacket.getTargetProtocolAddress()); |
| 558 | arpReply.setSenderHardwareAddress(myMAC.toBytes()); |
| 559 | |
| 560 | // Ethernet Frame. |
| 561 | Ethernet ethReply = new Ethernet(); |
| 562 | ethReply.setSourceMACAddress(myMAC); |
| 563 | ethReply.setDestinationMACAddress(packet.getSourceMAC()); |
| 564 | ethReply.setEtherType(Ethernet.TYPE_ARP); |
| 565 | ethReply.setVlanID(packet.getVlanID()); |
| 566 | |
| 567 | ethReply.setPayload(arpReply); |
| 568 | sendReply(context, ethReply); |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * Integrates hosts learned through DHCP into topology. |
| 573 | * @param context context of the incoming message |
| 574 | * @param ipAssigned IP Address assigned to the host by DHCP Manager |
| 575 | */ |
| 576 | private void discoverHost(PacketContext context, Ip4Address ipAssigned) { |
| 577 | Ethernet packet = context.inPacket().parsed(); |
| 578 | MacAddress mac = packet.getSourceMAC(); |
| 579 | VlanId vlanId = VlanId.vlanId(packet.getVlanID()); |
| 580 | HostLocation hostLocation = new HostLocation(context.inPacket().receivedFrom(), 0); |
| 581 | |
| 582 | Set<IpAddress> ips = new HashSet<>(); |
| 583 | ips.add(ipAssigned); |
| 584 | |
| 585 | HostId hostId = HostId.hostId(mac, vlanId); |
| 586 | DefaultHostDescription desc = new DefaultHostDescription(mac, vlanId, hostLocation, ips); |
| 587 | hostProviderService.hostDetected(hostId, desc); |
| 588 | } |
| 589 | |
| 590 | |
| 591 | @Override |
| 592 | public void process(PacketContext context) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 593 | Ethernet packet = context.inPacket().parsed(); |
| 594 | if (packet == null) { |
| 595 | return; |
| 596 | } |
| 597 | |
| 598 | if (packet.getEtherType() == Ethernet.TYPE_IPV4) { |
| 599 | IPv4 ipv4Packet = (IPv4) packet.getPayload(); |
| 600 | |
| 601 | if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_UDP) { |
| 602 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 603 | |
| 604 | if (udpPacket.getDestinationPort() == UDP.DHCP_SERVER_PORT && |
| 605 | udpPacket.getSourcePort() == UDP.DHCP_CLIENT_PORT) { |
| 606 | // This is meant for the dhcp server so process the packet here. |
| 607 | |
| 608 | DHCP dhcpPayload = (DHCP) udpPacket.getPayload(); |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 609 | processDhcpPacket(context, dhcpPayload); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | } else if (packet.getEtherType() == Ethernet.TYPE_ARP) { |
| 613 | ARP arpPacket = (ARP) packet.getPayload(); |
| 614 | |
| 615 | if ((arpPacket.getOpCode() == ARP.OP_REQUEST) && |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 616 | Objects.equals(myIP, Ip4Address.valueOf(arpPacket.getTargetProtocolAddress()))) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 617 | |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 618 | processArpPacket(context, packet); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 619 | |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | private class InternalConfigListener implements NetworkConfigListener { |
| 626 | |
| 627 | /** |
| 628 | * Reconfigures the DHCP Server according to the configuration parameters passed. |
| 629 | * |
| 630 | * @param cfg configuration object |
| 631 | */ |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 632 | private void reconfigureNetwork(DhcpConfig cfg) { |
Thomas Vachuska | 0009044 | 2015-09-11 18:08:04 -0700 | [diff] [blame] | 633 | if (cfg == null) { |
| 634 | return; |
| 635 | } |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 636 | if (cfg.ip() != null) { |
| 637 | myIP = cfg.ip(); |
| 638 | } |
| 639 | if (cfg.mac() != null) { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 640 | myMAC = cfg.mac(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 641 | } |
| 642 | if (cfg.subnetMask() != null) { |
| 643 | subnetMask = cfg.subnetMask(); |
| 644 | } |
| 645 | if (cfg.broadcastAddress() != null) { |
| 646 | broadcastAddress = cfg.broadcastAddress(); |
| 647 | } |
| 648 | if (cfg.routerAddress() != null) { |
| 649 | routerAddress = cfg.routerAddress(); |
| 650 | } |
| 651 | if (cfg.domainServer() != null) { |
| 652 | domainServer = cfg.domainServer(); |
| 653 | } |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 654 | if (cfg.ttl() != -1) { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 655 | packetTTL = (byte) cfg.ttl(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 656 | } |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 657 | if (cfg.leaseTime() != -1) { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 658 | leaseTime = cfg.leaseTime(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 659 | } |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 660 | if (cfg.renewTime() != -1) { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 661 | renewalTime = cfg.renewTime(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 662 | } |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 663 | if (cfg.rebindTime() != -1) { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 664 | rebindingTime = cfg.rebindTime(); |
| 665 | } |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 666 | if (cfg.defaultTimeout() != -1) { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 667 | dhcpStore.setDefaultTimeoutForPurge(cfg.defaultTimeout()); |
| 668 | } |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 669 | if (cfg.timerDelay() != -1) { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 670 | timerDelay = cfg.timerDelay(); |
| 671 | } |
| 672 | if ((cfg.startIp() != null) && (cfg.endIp() != null)) { |
| 673 | dhcpStore.populateIPPoolfromRange(cfg.startIp(), cfg.endIp()); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 677 | |
| 678 | @Override |
| 679 | public void event(NetworkConfigEvent event) { |
| 680 | |
| 681 | if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 682 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) && |
| 683 | event.configClass().equals(DhcpConfig.class)) { |
| 684 | |
| 685 | DhcpConfig cfg = cfgService.getConfig(appId, DhcpConfig.class); |
| 686 | reconfigureNetwork(cfg); |
| 687 | log.info("Reconfigured"); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | private class InternalHostProvider extends AbstractProvider implements HostProvider { |
| 693 | |
| 694 | /** |
| 695 | * Creates a provider with the supplier identifier. |
| 696 | */ |
| 697 | protected InternalHostProvider() { |
| 698 | super(PID); |
| 699 | } |
| 700 | |
| 701 | @Override |
| 702 | public void triggerProbe(Host host) { |
| 703 | // nothing to do |
| 704 | } |
| 705 | } |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 706 | |
| 707 | private class PurgeListTask implements TimerTask { |
| 708 | |
| 709 | @Override |
| 710 | public void run(Timeout to) { |
| 711 | IpAssignment ipAssignment; |
| 712 | Date dateNow = new Date(); |
| 713 | |
samanwita pal | 0bff430 | 2015-09-15 13:37:00 -0700 | [diff] [blame] | 714 | Map<HostId, IpAssignment> ipAssignmentMap = dhcpStore.listAllMapping(); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 715 | for (Map.Entry<HostId, IpAssignment> entry: ipAssignmentMap.entrySet()) { |
| 716 | ipAssignment = entry.getValue(); |
| 717 | |
| 718 | long timeLapsed = dateNow.getTime() - ipAssignment.timestamp().getTime(); |
| 719 | if ((ipAssignment.assignmentStatus() != IpAssignment.AssignmentStatus.Option_Expired) && |
| 720 | (ipAssignment.leasePeriod() > 0) && (timeLapsed > (ipAssignment.leasePeriodMs()))) { |
| 721 | |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 722 | Ip4Address ip4Address = dhcpStore.releaseIP(entry.getKey()); |
| 723 | if (ip4Address != null) { |
| 724 | hostProviderService.removeIpFromHost(entry.getKey(), ipAssignment.ipAddress()); |
| 725 | } |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 726 | } |
| 727 | } |
| 728 | timeout = Timer.getTimer().newTimeout(new PurgeListTask(), timerDelay, TimeUnit.MINUTES); |
| 729 | } |
| 730 | } |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 731 | } |