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