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; |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 77 | import java.util.Objects; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 78 | import java.util.Set; |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 79 | import java.util.concurrent.TimeUnit; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 80 | |
| 81 | import static org.onlab.packet.MacAddress.valueOf; |
| 82 | import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
| 83 | |
| 84 | /** |
| 85 | * Skeletal ONOS DHCP Server application. |
| 86 | */ |
| 87 | @Component(immediate = true) |
| 88 | @Service |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 89 | public class DhcpManager implements DhcpService { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 90 | |
| 91 | private static final ProviderId PID = new ProviderId("of", "org.onosproject.dhcp", true); |
| 92 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 93 | |
Thomas Vachuska | 0009044 | 2015-09-11 18:08:04 -0700 | [diff] [blame] | 94 | private final InternalConfigListener cfgListener = new InternalConfigListener(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 95 | |
| 96 | private final Set<ConfigFactory> factories = ImmutableSet.of( |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 97 | new ConfigFactory<ApplicationId, DhcpConfig>(APP_SUBJECT_FACTORY, |
| 98 | DhcpConfig.class, |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 99 | "dhcp") { |
| 100 | @Override |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 101 | public DhcpConfig createConfig() { |
| 102 | return new DhcpConfig(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 103 | } |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 104 | } |
| 105 | ); |
| 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 107 | protected NetworkConfigRegistry cfgService; |
| 108 | |
| 109 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 110 | protected PacketService packetService; |
| 111 | |
| 112 | private DHCPPacketProcessor processor = new DHCPPacketProcessor(); |
| 113 | |
| 114 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 115 | protected CoreService coreService; |
| 116 | |
| 117 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 118 | protected DhcpStore dhcpStore; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 119 | |
| 120 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 121 | protected HostProviderRegistry hostProviderRegistry; |
| 122 | |
| 123 | protected HostProviderService hostProviderService; |
| 124 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 125 | private final HostProvider hostProvider = new InternalHostProvider(); |
| 126 | |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 127 | private ApplicationId appId; |
| 128 | |
| 129 | // Hardcoded values are default values. |
| 130 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 131 | private static Ip4Address myIP = Ip4Address.valueOf("10.0.0.2"); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 132 | |
| 133 | private static MacAddress myMAC = valueOf("4f:4f:4f:4f:4f:4f"); |
| 134 | |
| 135 | /** |
| 136 | * leaseTime - 10 mins or 600s. |
| 137 | * renewalTime - 5 mins or 300s. |
| 138 | * rebindingTime - 6 mins or 360s. |
| 139 | */ |
| 140 | |
| 141 | private static int leaseTime = 600; |
| 142 | |
| 143 | private static int renewalTime = 300; |
| 144 | |
| 145 | private static int rebindingTime = 360; |
| 146 | |
| 147 | private static byte packetTTL = (byte) 127; |
| 148 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 149 | private static Ip4Address subnetMask = Ip4Address.valueOf("255.0.0.0"); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 150 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 151 | private static Ip4Address broadcastAddress = Ip4Address.valueOf("10.255.255.255"); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 152 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 153 | private static Ip4Address routerAddress = Ip4Address.valueOf("10.0.0.2"); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 154 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 155 | private static Ip4Address domainServer = Ip4Address.valueOf("10.0.0.2"); |
| 156 | |
| 157 | protected Timeout timeout; |
| 158 | |
| 159 | protected static int timerDelay = 2; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 160 | |
| 161 | @Activate |
| 162 | protected void activate() { |
| 163 | // start the dhcp server |
| 164 | appId = coreService.registerApplication("org.onosproject.dhcp"); |
| 165 | |
| 166 | cfgService.addListener(cfgListener); |
| 167 | factories.forEach(cfgService::registerConfigFactory); |
Thomas Vachuska | 0009044 | 2015-09-11 18:08:04 -0700 | [diff] [blame] | 168 | cfgListener.reconfigureNetwork(cfgService.getConfig(appId, DhcpConfig.class)); |
Thomas Vachuska | 0009044 | 2015-09-11 18:08:04 -0700 | [diff] [blame] | 169 | |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 170 | hostProviderService = hostProviderRegistry.register(hostProvider); |
Thomas Vachuska | 2a645d4 | 2015-09-11 18:58:36 -0700 | [diff] [blame] | 171 | packetService.addProcessor(processor, PacketProcessor.director(0)); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 172 | requestPackets(); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 173 | timeout = Timer.getTimer().newTimeout(new PurgeListTask(), timerDelay, TimeUnit.MINUTES); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 174 | log.info("Started"); |
| 175 | } |
| 176 | |
| 177 | @Deactivate |
| 178 | protected void deactivate() { |
| 179 | cfgService.removeListener(cfgListener); |
| 180 | factories.forEach(cfgService::unregisterConfigFactory); |
| 181 | packetService.removeProcessor(processor); |
| 182 | hostProviderRegistry.unregister(hostProvider); |
| 183 | hostProviderService = null; |
| 184 | cancelPackets(); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 185 | timeout.cancel(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 186 | log.info("Stopped"); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Request packet in via PacketService. |
| 191 | */ |
| 192 | private void requestPackets() { |
| 193 | |
| 194 | TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder() |
| 195 | .matchEthType(Ethernet.TYPE_IPV4) |
| 196 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
| 197 | .matchUdpDst(TpPort.tpPort(UDP.DHCP_SERVER_PORT)) |
| 198 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_CLIENT_PORT)); |
| 199 | packetService.requestPackets(selectorServer.build(), PacketPriority.CONTROL, appId); |
| 200 | |
| 201 | selectorServer = DefaultTrafficSelector.builder() |
| 202 | .matchEthType(Ethernet.TYPE_ARP); |
| 203 | packetService.requestPackets(selectorServer.build(), PacketPriority.CONTROL, appId); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Cancel requested packets in via packet service. |
| 208 | */ |
| 209 | private void cancelPackets() { |
| 210 | TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder() |
| 211 | .matchEthType(Ethernet.TYPE_IPV4) |
| 212 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
| 213 | .matchUdpDst(TpPort.tpPort(UDP.DHCP_SERVER_PORT)) |
| 214 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_CLIENT_PORT)); |
| 215 | packetService.cancelPackets(selectorServer.build(), PacketPriority.CONTROL, appId); |
| 216 | |
| 217 | selectorServer = DefaultTrafficSelector.builder() |
| 218 | .matchEthType(Ethernet.TYPE_ARP); |
| 219 | packetService.cancelPackets(selectorServer.build(), PacketPriority.CONTROL, appId); |
| 220 | } |
| 221 | |
| 222 | @Override |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 223 | public Map<HostId, IpAssignment> listMapping() { |
samanwita pal | 0bff430 | 2015-09-15 13:37:00 -0700 | [diff] [blame] | 224 | return dhcpStore.listAssignedMapping(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public int getLeaseTime() { |
| 229 | return leaseTime; |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public int getRenewalTime() { |
| 234 | return renewalTime; |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public int getRebindingTime() { |
| 239 | return rebindingTime; |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public boolean setStaticMapping(MacAddress macID, Ip4Address ipAddress) { |
| 244 | return dhcpStore.assignStaticIP(macID, ipAddress); |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public boolean removeStaticMapping(MacAddress macID) { |
| 249 | return dhcpStore.removeStaticIP(macID); |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public Iterable<Ip4Address> getAvailableIPs() { |
| 254 | return dhcpStore.getAvailableIPs(); |
| 255 | } |
| 256 | |
| 257 | private class DHCPPacketProcessor implements PacketProcessor { |
| 258 | |
| 259 | /** |
| 260 | * Builds the DHCP Reply packet. |
| 261 | * |
| 262 | * @param packet the incoming Ethernet frame |
| 263 | * @param ipOffered the IP offered by the DHCP Server |
| 264 | * @param outgoingMessageType the message type of the outgoing packet |
| 265 | * @return the Ethernet reply frame |
| 266 | */ |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 267 | private Ethernet buildReply(Ethernet packet, Ip4Address ipOffered, byte outgoingMessageType) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 268 | |
| 269 | // Ethernet Frame. |
| 270 | Ethernet ethReply = new Ethernet(); |
| 271 | ethReply.setSourceMACAddress(myMAC); |
| 272 | ethReply.setDestinationMACAddress(packet.getSourceMAC()); |
| 273 | ethReply.setEtherType(Ethernet.TYPE_IPV4); |
| 274 | ethReply.setVlanID(packet.getVlanID()); |
| 275 | |
| 276 | // IP Packet |
| 277 | IPv4 ipv4Packet = (IPv4) packet.getPayload(); |
| 278 | IPv4 ipv4Reply = new IPv4(); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 279 | ipv4Reply.setSourceAddress(myIP.toInt()); |
| 280 | ipv4Reply.setDestinationAddress(ipOffered.toInt()); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 281 | ipv4Reply.setTtl(packetTTL); |
| 282 | |
| 283 | // UDP Datagram. |
| 284 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 285 | UDP udpReply = new UDP(); |
| 286 | udpReply.setSourcePort((byte) UDP.DHCP_SERVER_PORT); |
| 287 | udpReply.setDestinationPort((byte) UDP.DHCP_CLIENT_PORT); |
| 288 | |
| 289 | // DHCP Payload. |
| 290 | DHCP dhcpPacket = (DHCP) udpPacket.getPayload(); |
| 291 | DHCP dhcpReply = new DHCP(); |
| 292 | dhcpReply.setOpCode(DHCP.OPCODE_REPLY); |
| 293 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 294 | dhcpReply.setYourIPAddress(ipOffered.toInt()); |
| 295 | dhcpReply.setServerIPAddress(myIP.toInt()); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 296 | |
| 297 | dhcpReply.setTransactionId(dhcpPacket.getTransactionId()); |
| 298 | dhcpReply.setClientHardwareAddress(dhcpPacket.getClientHardwareAddress()); |
| 299 | dhcpReply.setHardwareType(DHCP.HWTYPE_ETHERNET); |
| 300 | dhcpReply.setHardwareAddressLength((byte) 6); |
| 301 | |
| 302 | // DHCP Options. |
| 303 | DHCPOption option = new DHCPOption(); |
| 304 | List<DHCPOption> optionList = new ArrayList<>(); |
| 305 | |
| 306 | // DHCP Message Type. |
| 307 | option.setCode(DHCP.DHCPOptionCode.OptionCode_MessageType.getValue()); |
| 308 | option.setLength((byte) 1); |
| 309 | byte[] optionData = {outgoingMessageType}; |
| 310 | option.setData(optionData); |
| 311 | optionList.add(option); |
| 312 | |
| 313 | // DHCP Server Identifier. |
| 314 | option = new DHCPOption(); |
| 315 | option.setCode(DHCP.DHCPOptionCode.OptionCode_DHCPServerIp.getValue()); |
| 316 | option.setLength((byte) 4); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 317 | option.setData(myIP.toOctets()); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 318 | optionList.add(option); |
| 319 | |
| 320 | // IP Address Lease Time. |
| 321 | option = new DHCPOption(); |
| 322 | option.setCode(DHCP.DHCPOptionCode.OptionCode_LeaseTime.getValue()); |
| 323 | option.setLength((byte) 4); |
| 324 | option.setData(ByteBuffer.allocate(4).putInt(leaseTime).array()); |
| 325 | optionList.add(option); |
| 326 | |
| 327 | // IP Address Renewal Time. |
| 328 | option = new DHCPOption(); |
| 329 | option.setCode(DHCP.DHCPOptionCode.OptionCode_RenewalTime.getValue()); |
| 330 | option.setLength((byte) 4); |
| 331 | option.setData(ByteBuffer.allocate(4).putInt(renewalTime).array()); |
| 332 | optionList.add(option); |
| 333 | |
| 334 | // IP Address Rebinding Time. |
| 335 | option = new DHCPOption(); |
| 336 | option.setCode(DHCP.DHCPOptionCode.OPtionCode_RebindingTime.getValue()); |
| 337 | option.setLength((byte) 4); |
| 338 | option.setData(ByteBuffer.allocate(4).putInt(rebindingTime).array()); |
| 339 | optionList.add(option); |
| 340 | |
| 341 | // Subnet Mask. |
| 342 | option = new DHCPOption(); |
| 343 | option.setCode(DHCP.DHCPOptionCode.OptionCode_SubnetMask.getValue()); |
| 344 | option.setLength((byte) 4); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 345 | option.setData(subnetMask.toOctets()); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 346 | optionList.add(option); |
| 347 | |
| 348 | // Broadcast Address. |
| 349 | option = new DHCPOption(); |
| 350 | option.setCode(DHCP.DHCPOptionCode.OptionCode_BroadcastAddress.getValue()); |
| 351 | option.setLength((byte) 4); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 352 | option.setData(broadcastAddress.toOctets()); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 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); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 359 | option.setData(routerAddress.toOctets()); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 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); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 366 | option.setData(domainServer.toOctets()); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 367 | optionList.add(option); |
| 368 | |
| 369 | // End Option. |
| 370 | option = new DHCPOption(); |
| 371 | option.setCode(DHCP.DHCPOptionCode.OptionCode_END.getValue()); |
| 372 | option.setLength((byte) 1); |
| 373 | optionList.add(option); |
| 374 | |
| 375 | dhcpReply.setOptions(optionList); |
| 376 | |
| 377 | udpReply.setPayload(dhcpReply); |
| 378 | ipv4Reply.setPayload(udpReply); |
| 379 | ethReply.setPayload(ipv4Reply); |
| 380 | |
| 381 | return ethReply; |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Sends the Ethernet reply frame via the Packet Service. |
| 386 | * |
| 387 | * @param context the context of the incoming frame |
| 388 | * @param reply the Ethernet reply frame |
| 389 | */ |
| 390 | private void sendReply(PacketContext context, Ethernet reply) { |
| 391 | if (reply != null) { |
| 392 | TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder(); |
| 393 | ConnectPoint sourcePoint = context.inPacket().receivedFrom(); |
| 394 | builder.setOutput(sourcePoint.port()); |
Thomas Vachuska | 0009044 | 2015-09-11 18:08:04 -0700 | [diff] [blame] | 395 | context.block(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 396 | packetService.emit(new DefaultOutboundPacket(sourcePoint.deviceId(), |
samanwita pal | 8969cbe | 2015-09-04 13:31:30 -0700 | [diff] [blame] | 397 | builder.build(), ByteBuffer.wrap(reply.serialize()))); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Processes the DHCP Payload and initiates a reply to the client. |
| 403 | * |
| 404 | * @param context context of the incoming message |
| 405 | * @param dhcpPayload the extracted DHCP payload |
| 406 | */ |
| 407 | private void processDHCPPacket(PacketContext context, DHCP dhcpPayload) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 408 | Ethernet packet = context.inPacket().parsed(); |
| 409 | boolean flagIfRequestedIP = false; |
| 410 | boolean flagIfServerIP = false; |
| 411 | Ip4Address requestedIP = Ip4Address.valueOf("0.0.0.0"); |
| 412 | Ip4Address serverIP = Ip4Address.valueOf("0.0.0.0"); |
| 413 | |
| 414 | if (dhcpPayload != null) { |
| 415 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 416 | DHCPPacketType incomingPacketType = DHCPPacketType.getType(0); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 417 | for (DHCPOption option : dhcpPayload.getOptions()) { |
| 418 | if (option.getCode() == DHCP.DHCPOptionCode.OptionCode_MessageType.getValue()) { |
| 419 | byte[] data = option.getData(); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 420 | incomingPacketType = DHCPPacketType.getType(data[0]); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 421 | } |
| 422 | if (option.getCode() == DHCP.DHCPOptionCode.OptionCode_RequestedIP.getValue()) { |
| 423 | byte[] data = option.getData(); |
| 424 | requestedIP = Ip4Address.valueOf(data); |
| 425 | flagIfRequestedIP = true; |
| 426 | } |
| 427 | if (option.getCode() == DHCP.DHCPOptionCode.OptionCode_DHCPServerIp.getValue()) { |
| 428 | byte[] data = option.getData(); |
| 429 | serverIP = Ip4Address.valueOf(data); |
| 430 | flagIfServerIP = true; |
| 431 | } |
| 432 | } |
samanwita pal | 8969cbe | 2015-09-04 13:31:30 -0700 | [diff] [blame] | 433 | DHCPPacketType outgoingPacketType; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 434 | MacAddress clientMAC = new MacAddress(dhcpPayload.getClientHardwareAddress()); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 435 | VlanId vlanId = VlanId.vlanId(packet.getVlanID()); |
| 436 | HostId hostId = HostId.hostId(clientMAC, vlanId); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 437 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 438 | if (incomingPacketType.getValue() == DHCPPacketType.DHCPDISCOVER.getValue()) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 439 | |
samanwita pal | 8969cbe | 2015-09-04 13:31:30 -0700 | [diff] [blame] | 440 | outgoingPacketType = DHCPPacketType.DHCPOFFER; |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 441 | Ip4Address ipOffered = dhcpStore.suggestIP(hostId, requestedIP); |
| 442 | if (ipOffered != null) { |
| 443 | Ethernet ethReply = buildReply(packet, ipOffered, |
| 444 | (byte) outgoingPacketType.getValue()); |
| 445 | sendReply(context, ethReply); |
| 446 | } |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 447 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 448 | } else if (incomingPacketType.getValue() == DHCPPacketType.DHCPREQUEST.getValue()) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 449 | |
samanwita pal | 8969cbe | 2015-09-04 13:31:30 -0700 | [diff] [blame] | 450 | outgoingPacketType = DHCPPacketType.DHCPACK; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 451 | |
| 452 | if (flagIfServerIP && flagIfRequestedIP) { |
| 453 | // SELECTING state |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 454 | if (myIP.equals(serverIP) && |
| 455 | dhcpStore.assignIP(hostId, requestedIP, leaseTime)) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 456 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 457 | Ethernet ethReply = buildReply(packet, requestedIP, |
samanwita pal | 8969cbe | 2015-09-04 13:31:30 -0700 | [diff] [blame] | 458 | (byte) outgoingPacketType.getValue()); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 459 | sendReply(context, ethReply); |
| 460 | discoverHost(context, requestedIP); |
| 461 | } |
| 462 | } else if (flagIfRequestedIP) { |
| 463 | // INIT-REBOOT state |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 464 | if (dhcpStore.assignIP(hostId, requestedIP, leaseTime)) { |
| 465 | Ethernet ethReply = buildReply(packet, requestedIP, |
samanwita pal | 8969cbe | 2015-09-04 13:31:30 -0700 | [diff] [blame] | 466 | (byte) outgoingPacketType.getValue()); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 467 | sendReply(context, ethReply); |
| 468 | discoverHost(context, requestedIP); |
| 469 | } |
| 470 | } else { |
| 471 | // RENEWING and REBINDING state |
| 472 | int ciaadr = dhcpPayload.getClientIPAddress(); |
| 473 | if (ciaadr != 0) { |
| 474 | Ip4Address clientIaddr = Ip4Address.valueOf(ciaadr); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 475 | if (dhcpStore.assignIP(hostId, clientIaddr, leaseTime)) { |
| 476 | Ethernet ethReply = buildReply(packet, clientIaddr, |
samanwita pal | 8969cbe | 2015-09-04 13:31:30 -0700 | [diff] [blame] | 477 | (byte) outgoingPacketType.getValue()); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 478 | sendReply(context, ethReply); |
| 479 | discoverHost(context, clientIaddr); |
| 480 | } |
| 481 | } |
| 482 | } |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 483 | } else if (incomingPacketType.getValue() == DHCPPacketType.DHCPRELEASE.getValue()) { |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 484 | Ip4Address ip4Address = dhcpStore.releaseIP(hostId); |
| 485 | if (ip4Address != null) { |
| 486 | hostProviderService.removeIpFromHost(hostId, ip4Address); |
| 487 | } |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 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) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 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) && |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 566 | Objects.equals(myIP, Ip4Address.valueOf(arpPacket.getTargetProtocolAddress()))) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 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 | */ |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 582 | private void reconfigureNetwork(DhcpConfig cfg) { |
Thomas Vachuska | 0009044 | 2015-09-11 18:08:04 -0700 | [diff] [blame] | 583 | if (cfg == null) { |
| 584 | return; |
| 585 | } |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 586 | if (cfg.ip() != null) { |
| 587 | myIP = cfg.ip(); |
| 588 | } |
| 589 | if (cfg.mac() != null) { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 590 | myMAC = cfg.mac(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 591 | } |
| 592 | if (cfg.subnetMask() != null) { |
| 593 | subnetMask = cfg.subnetMask(); |
| 594 | } |
| 595 | if (cfg.broadcastAddress() != null) { |
| 596 | broadcastAddress = cfg.broadcastAddress(); |
| 597 | } |
| 598 | if (cfg.routerAddress() != null) { |
| 599 | routerAddress = cfg.routerAddress(); |
| 600 | } |
| 601 | if (cfg.domainServer() != null) { |
| 602 | domainServer = cfg.domainServer(); |
| 603 | } |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 604 | if (cfg.ttl() != -1) { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 605 | packetTTL = (byte) cfg.ttl(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 606 | } |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 607 | if (cfg.leaseTime() != -1) { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 608 | leaseTime = cfg.leaseTime(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 609 | } |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 610 | if (cfg.renewTime() != -1) { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 611 | renewalTime = cfg.renewTime(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 612 | } |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 613 | if (cfg.rebindTime() != -1) { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 614 | rebindingTime = cfg.rebindTime(); |
| 615 | } |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 616 | if (cfg.defaultTimeout() != -1) { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 617 | dhcpStore.setDefaultTimeoutForPurge(cfg.defaultTimeout()); |
| 618 | } |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 619 | if (cfg.timerDelay() != -1) { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 620 | timerDelay = cfg.timerDelay(); |
| 621 | } |
| 622 | if ((cfg.startIp() != null) && (cfg.endIp() != null)) { |
| 623 | dhcpStore.populateIPPoolfromRange(cfg.startIp(), cfg.endIp()); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 627 | |
| 628 | @Override |
| 629 | public void event(NetworkConfigEvent event) { |
| 630 | |
| 631 | if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 632 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) && |
| 633 | event.configClass().equals(DhcpConfig.class)) { |
| 634 | |
| 635 | DhcpConfig cfg = cfgService.getConfig(appId, DhcpConfig.class); |
| 636 | reconfigureNetwork(cfg); |
| 637 | log.info("Reconfigured"); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | private class InternalHostProvider extends AbstractProvider implements HostProvider { |
| 643 | |
| 644 | /** |
| 645 | * Creates a provider with the supplier identifier. |
| 646 | */ |
| 647 | protected InternalHostProvider() { |
| 648 | super(PID); |
| 649 | } |
| 650 | |
| 651 | @Override |
| 652 | public void triggerProbe(Host host) { |
| 653 | // nothing to do |
| 654 | } |
| 655 | } |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 656 | |
| 657 | private class PurgeListTask implements TimerTask { |
| 658 | |
| 659 | @Override |
| 660 | public void run(Timeout to) { |
| 661 | IpAssignment ipAssignment; |
| 662 | Date dateNow = new Date(); |
| 663 | |
samanwita pal | 0bff430 | 2015-09-15 13:37:00 -0700 | [diff] [blame] | 664 | Map<HostId, IpAssignment> ipAssignmentMap = dhcpStore.listAllMapping(); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 665 | for (Map.Entry<HostId, IpAssignment> entry: ipAssignmentMap.entrySet()) { |
| 666 | ipAssignment = entry.getValue(); |
| 667 | |
| 668 | long timeLapsed = dateNow.getTime() - ipAssignment.timestamp().getTime(); |
| 669 | if ((ipAssignment.assignmentStatus() != IpAssignment.AssignmentStatus.Option_Expired) && |
| 670 | (ipAssignment.leasePeriod() > 0) && (timeLapsed > (ipAssignment.leasePeriodMs()))) { |
| 671 | |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 672 | Ip4Address ip4Address = dhcpStore.releaseIP(entry.getKey()); |
| 673 | if (ip4Address != null) { |
| 674 | hostProviderService.removeIpFromHost(entry.getKey(), ipAssignment.ipAddress()); |
| 675 | } |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | timeout = Timer.getTimer().newTimeout(new PurgeListTask(), timerDelay, TimeUnit.MINUTES); |
| 679 | } |
| 680 | } |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 681 | } |