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