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