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