samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 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 | |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 18 | import com.google.common.base.Strings; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Yuta HIGUCHI | 19afc03 | 2017-05-20 23:44:17 -0700 | [diff] [blame] | 20 | |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Modified; |
| 25 | import org.apache.felix.scr.annotations.Property; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Reference; |
| 27 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 28 | import org.apache.felix.scr.annotations.Service; |
| 29 | import org.onlab.packet.ARP; |
| 30 | import org.onlab.packet.DHCP; |
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; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 39 | import org.onlab.packet.dhcp.DhcpOption; |
Yuta HIGUCHI | 19afc03 | 2017-05-20 23:44:17 -0700 | [diff] [blame] | 40 | import org.onlab.util.SharedScheduledExecutors; |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 41 | import org.onlab.util.Tools; |
| 42 | import org.onosproject.cfg.ComponentConfigService; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 43 | import org.onosproject.core.ApplicationId; |
| 44 | import org.onosproject.core.CoreService; |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 45 | import org.onosproject.dhcp.DhcpService; |
| 46 | import org.onosproject.dhcp.DhcpStore; |
| 47 | import org.onosproject.dhcp.IpAssignment; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 48 | import org.onosproject.net.ConnectPoint; |
| 49 | import org.onosproject.net.Host; |
| 50 | import org.onosproject.net.HostId; |
| 51 | import org.onosproject.net.HostLocation; |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 52 | import org.onosproject.net.config.ConfigFactory; |
| 53 | import org.onosproject.net.config.NetworkConfigEvent; |
| 54 | import org.onosproject.net.config.NetworkConfigListener; |
| 55 | import org.onosproject.net.config.NetworkConfigRegistry; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 56 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 57 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 58 | import org.onosproject.net.flow.TrafficSelector; |
| 59 | import org.onosproject.net.flow.TrafficTreatment; |
| 60 | import org.onosproject.net.host.DefaultHostDescription; |
| 61 | import org.onosproject.net.host.HostProvider; |
| 62 | import org.onosproject.net.host.HostProviderRegistry; |
| 63 | import org.onosproject.net.host.HostProviderService; |
| 64 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 65 | import org.onosproject.net.packet.PacketContext; |
| 66 | import org.onosproject.net.packet.PacketPriority; |
| 67 | import org.onosproject.net.packet.PacketProcessor; |
| 68 | import org.onosproject.net.packet.PacketService; |
| 69 | import org.onosproject.net.provider.AbstractProvider; |
| 70 | import org.onosproject.net.provider.ProviderId; |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 71 | import org.osgi.service.component.ComponentContext; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 72 | import org.slf4j.Logger; |
| 73 | import org.slf4j.LoggerFactory; |
| 74 | |
| 75 | import java.nio.ByteBuffer; |
| 76 | import java.util.ArrayList; |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 77 | import java.util.Date; |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 78 | import java.util.Dictionary; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 79 | import java.util.HashSet; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 80 | |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 81 | import java.util.List; |
| 82 | import java.util.Map; |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 83 | import java.util.Objects; |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 84 | import java.util.Optional; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 85 | import java.util.Set; |
Yuta HIGUCHI | 19afc03 | 2017-05-20 23:44:17 -0700 | [diff] [blame] | 86 | import java.util.concurrent.ScheduledFuture; |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 87 | import java.util.concurrent.TimeUnit; |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 88 | |
| 89 | import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_DHCPServerIp; |
| 90 | import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_MessageType; |
| 91 | import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_RequestedIP; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 92 | import static org.onlab.packet.MacAddress.valueOf; |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 93 | import static org.onosproject.dhcp.IpAssignment.AssignmentStatus.Option_RangeNotEnforced; |
| 94 | import static org.onosproject.dhcp.IpAssignment.AssignmentStatus.Option_Requested; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 95 | import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
| 96 | |
| 97 | /** |
| 98 | * Skeletal ONOS DHCP Server application. |
| 99 | */ |
| 100 | @Component(immediate = true) |
| 101 | @Service |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 102 | public class DhcpManager implements DhcpService { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 103 | |
| 104 | private static final ProviderId PID = new ProviderId("of", "org.onosproject.dhcp", true); |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 105 | private static final String ALLOW_HOST_DISCOVERY = "allowHostDiscovery"; |
| 106 | private static final boolean DEFAULT_ALLOW_HOST_DISCOVERY = false; |
| 107 | |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 108 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 109 | |
Thomas Vachuska | 0009044 | 2015-09-11 18:08:04 -0700 | [diff] [blame] | 110 | private final InternalConfigListener cfgListener = new InternalConfigListener(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 111 | |
| 112 | private final Set<ConfigFactory> factories = ImmutableSet.of( |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 113 | new ConfigFactory<ApplicationId, DhcpConfig>(APP_SUBJECT_FACTORY, |
| 114 | DhcpConfig.class, |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 115 | "dhcp") { |
| 116 | @Override |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 117 | public DhcpConfig createConfig() { |
| 118 | return new DhcpConfig(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 119 | } |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 120 | } |
| 121 | ); |
| 122 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 123 | protected NetworkConfigRegistry cfgService; |
| 124 | |
| 125 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 126 | protected PacketService packetService; |
| 127 | |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 128 | private DhcpPacketProcessor processor = new DhcpPacketProcessor(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 129 | |
| 130 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 131 | protected CoreService coreService; |
| 132 | |
| 133 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 134 | protected DhcpStore dhcpStore; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 135 | |
| 136 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 137 | protected HostProviderRegistry hostProviderRegistry; |
| 138 | |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 139 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 140 | protected ComponentConfigService componentConfigService; |
| 141 | |
| 142 | @Property(name = ALLOW_HOST_DISCOVERY, boolValue = DEFAULT_ALLOW_HOST_DISCOVERY, |
| 143 | label = "Allow host discovery from DHCP request") |
| 144 | private boolean allowHostDiscovery = DEFAULT_ALLOW_HOST_DISCOVERY; |
| 145 | |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 146 | protected HostProviderService hostProviderService; |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 147 | private final HostProvider hostProvider = new InternalHostProvider(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 148 | private ApplicationId appId; |
| 149 | |
| 150 | // Hardcoded values are default values. |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 151 | /** |
| 152 | * leaseTime - 10 mins or 600s. |
| 153 | * renewalTime - 5 mins or 300s. |
| 154 | * rebindingTime - 6 mins or 360s. |
| 155 | */ |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 156 | private static int leaseTime = 600; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 157 | private static int renewalTime = 300; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 158 | private static int rebindingTime = 360; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 159 | private static byte packetTTL = (byte) 127; |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 160 | private static Ip4Address subnetMask = Ip4Address.valueOf("255.0.0.0"); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 161 | private static Ip4Address broadcastAddress = Ip4Address.valueOf("10.255.255.255"); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 162 | private static Ip4Address routerAddress = Ip4Address.valueOf("10.0.0.2"); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 163 | private static Ip4Address domainServer = Ip4Address.valueOf("10.0.0.2"); |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 164 | private static Ip4Address myIP = Ip4Address.valueOf("10.0.0.2"); |
Rafal Szalecki | 9638ef6 | 2017-02-14 15:05:51 +0100 | [diff] [blame] | 165 | private static MacAddress myMAC = valueOf("4e:4f:4f:4f:4f:4f"); |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 166 | private static final Ip4Address IP_BROADCAST = Ip4Address.valueOf("255.255.255.255"); |
| 167 | |
Yuta HIGUCHI | 19afc03 | 2017-05-20 23:44:17 -0700 | [diff] [blame] | 168 | protected ScheduledFuture<?> timeout; |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 169 | protected static int timerDelay = 2; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 170 | |
| 171 | @Activate |
| 172 | protected void activate() { |
| 173 | // start the dhcp server |
| 174 | appId = coreService.registerApplication("org.onosproject.dhcp"); |
| 175 | |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 176 | componentConfigService.registerProperties(getClass()); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 177 | cfgService.addListener(cfgListener); |
| 178 | factories.forEach(cfgService::registerConfigFactory); |
Thomas Vachuska | 0009044 | 2015-09-11 18:08:04 -0700 | [diff] [blame] | 179 | cfgListener.reconfigureNetwork(cfgService.getConfig(appId, DhcpConfig.class)); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 180 | hostProviderService = hostProviderRegistry.register(hostProvider); |
Hyunsun Moon | 4f47a86 | 2016-08-18 11:30:59 -0700 | [diff] [blame] | 181 | packetService.addProcessor(processor, PacketProcessor.director(1)); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 182 | requestPackets(); |
Yuta HIGUCHI | 19afc03 | 2017-05-20 23:44:17 -0700 | [diff] [blame] | 183 | timeout = SharedScheduledExecutors.newTimeout(new PurgeListTask(), timerDelay, TimeUnit.MINUTES); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 184 | log.info("Started"); |
| 185 | } |
| 186 | |
| 187 | @Deactivate |
| 188 | protected void deactivate() { |
| 189 | cfgService.removeListener(cfgListener); |
| 190 | factories.forEach(cfgService::unregisterConfigFactory); |
| 191 | packetService.removeProcessor(processor); |
| 192 | hostProviderRegistry.unregister(hostProvider); |
| 193 | hostProviderService = null; |
| 194 | cancelPackets(); |
Yuta HIGUCHI | 19afc03 | 2017-05-20 23:44:17 -0700 | [diff] [blame] | 195 | timeout.cancel(true); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 196 | log.info("Stopped"); |
| 197 | } |
| 198 | |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 199 | @Modified |
| 200 | protected void modified(ComponentContext context) { |
| 201 | Dictionary<?, ?> properties = context.getProperties(); |
| 202 | |
| 203 | String updatedConfig = Tools.get(properties, ALLOW_HOST_DISCOVERY); |
| 204 | if (!Strings.isNullOrEmpty(updatedConfig)) { |
| 205 | allowHostDiscovery = Boolean.valueOf(updatedConfig); |
| 206 | log.info("Host discovery is set to {}", updatedConfig); |
| 207 | } |
| 208 | |
| 209 | log.info("Modified"); |
| 210 | } |
| 211 | |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 212 | /** |
| 213 | * Request packet in via PacketService. |
| 214 | */ |
| 215 | private void requestPackets() { |
| 216 | |
| 217 | TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder() |
| 218 | .matchEthType(Ethernet.TYPE_IPV4) |
| 219 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
| 220 | .matchUdpDst(TpPort.tpPort(UDP.DHCP_SERVER_PORT)) |
| 221 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_CLIENT_PORT)); |
| 222 | packetService.requestPackets(selectorServer.build(), PacketPriority.CONTROL, appId); |
| 223 | |
| 224 | selectorServer = DefaultTrafficSelector.builder() |
| 225 | .matchEthType(Ethernet.TYPE_ARP); |
| 226 | packetService.requestPackets(selectorServer.build(), PacketPriority.CONTROL, appId); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Cancel requested packets in via packet service. |
| 231 | */ |
| 232 | private void cancelPackets() { |
| 233 | TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder() |
| 234 | .matchEthType(Ethernet.TYPE_IPV4) |
| 235 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
| 236 | .matchUdpDst(TpPort.tpPort(UDP.DHCP_SERVER_PORT)) |
| 237 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_CLIENT_PORT)); |
| 238 | packetService.cancelPackets(selectorServer.build(), PacketPriority.CONTROL, appId); |
| 239 | |
| 240 | selectorServer = DefaultTrafficSelector.builder() |
| 241 | .matchEthType(Ethernet.TYPE_ARP); |
| 242 | packetService.cancelPackets(selectorServer.build(), PacketPriority.CONTROL, appId); |
| 243 | } |
| 244 | |
| 245 | @Override |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 246 | public Map<HostId, IpAssignment> listMapping() { |
samanwita pal | 0bff430 | 2015-09-15 13:37:00 -0700 | [diff] [blame] | 247 | return dhcpStore.listAssignedMapping(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public int getLeaseTime() { |
| 252 | return leaseTime; |
| 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public int getRenewalTime() { |
| 257 | return renewalTime; |
| 258 | } |
| 259 | |
| 260 | @Override |
| 261 | public int getRebindingTime() { |
| 262 | return rebindingTime; |
| 263 | } |
| 264 | |
| 265 | @Override |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 266 | public boolean setStaticMapping(MacAddress macAddress, IpAssignment ipAssignment) { |
| 267 | log.debug("setStaticMapping is called with Mac: {} IpAssignment: {}", |
| 268 | macAddress, ipAssignment); |
| 269 | return dhcpStore.assignStaticIP(macAddress, ipAssignment); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public boolean removeStaticMapping(MacAddress macID) { |
| 274 | return dhcpStore.removeStaticIP(macID); |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public Iterable<Ip4Address> getAvailableIPs() { |
| 279 | return dhcpStore.getAvailableIPs(); |
| 280 | } |
| 281 | |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 282 | private class DhcpPacketProcessor implements PacketProcessor { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 283 | |
| 284 | /** |
| 285 | * Builds the DHCP Reply packet. |
| 286 | * |
| 287 | * @param packet the incoming Ethernet frame |
| 288 | * @param ipOffered the IP offered by the DHCP Server |
| 289 | * @param outgoingMessageType the message type of the outgoing packet |
| 290 | * @return the Ethernet reply frame |
| 291 | */ |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 292 | private Ethernet buildReply(Ethernet packet, Ip4Address ipOffered, byte outgoingMessageType) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 293 | |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 294 | // mandatory options |
| 295 | // TODO save and get the information below to/from IP assignment |
| 296 | Ip4Address dhcpServerReply = myIP; |
| 297 | Ip4Address subnetMaskReply = subnetMask; |
| 298 | Ip4Address broadcastReply = broadcastAddress; |
daniel | cd9deed | 2015-10-30 17:16:16 +0900 | [diff] [blame] | 299 | |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 300 | // optional options |
| 301 | Optional<Ip4Address> routerAddressReply = Optional.of(routerAddress); |
| 302 | Optional<Ip4Address> domainServerReply = Optional.of(domainServer); |
daniel | cd9deed | 2015-10-30 17:16:16 +0900 | [diff] [blame] | 303 | |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 304 | IpAssignment ipAssignment = dhcpStore.getIpAssignmentFromAllocationMap( |
| 305 | HostId.hostId(packet.getSourceMAC())); |
| 306 | |
| 307 | if (ipAssignment != null && |
| 308 | ipAssignment.assignmentStatus().equals(Option_RangeNotEnforced)) { |
daniel | cd9deed | 2015-10-30 17:16:16 +0900 | [diff] [blame] | 309 | subnetMaskReply = ipAssignment.subnetMask(); |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 310 | broadcastReply = ipAssignment.broadcast(); |
| 311 | routerAddressReply = Optional.ofNullable(ipAssignment.routerAddress()); |
| 312 | domainServerReply = Optional.ofNullable(ipAssignment.domainServer()); |
daniel | cd9deed | 2015-10-30 17:16:16 +0900 | [diff] [blame] | 313 | } |
| 314 | |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 315 | // Ethernet Frame. |
| 316 | Ethernet ethReply = new Ethernet(); |
| 317 | ethReply.setSourceMACAddress(myMAC); |
| 318 | ethReply.setDestinationMACAddress(packet.getSourceMAC()); |
| 319 | ethReply.setEtherType(Ethernet.TYPE_IPV4); |
| 320 | ethReply.setVlanID(packet.getVlanID()); |
| 321 | |
| 322 | // IP Packet |
| 323 | IPv4 ipv4Packet = (IPv4) packet.getPayload(); |
| 324 | IPv4 ipv4Reply = new IPv4(); |
daniel | cd9deed | 2015-10-30 17:16:16 +0900 | [diff] [blame] | 325 | ipv4Reply.setSourceAddress(dhcpServerReply.toInt()); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 326 | ipv4Reply.setDestinationAddress(ipOffered.toInt()); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 327 | ipv4Reply.setTtl(packetTTL); |
| 328 | |
| 329 | // UDP Datagram. |
| 330 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 331 | UDP udpReply = new UDP(); |
| 332 | udpReply.setSourcePort((byte) UDP.DHCP_SERVER_PORT); |
| 333 | udpReply.setDestinationPort((byte) UDP.DHCP_CLIENT_PORT); |
| 334 | |
| 335 | // DHCP Payload. |
| 336 | DHCP dhcpPacket = (DHCP) udpPacket.getPayload(); |
| 337 | DHCP dhcpReply = new DHCP(); |
| 338 | dhcpReply.setOpCode(DHCP.OPCODE_REPLY); |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 339 | dhcpReply.setFlags(dhcpPacket.getFlags()); |
| 340 | dhcpReply.setGatewayIPAddress(dhcpPacket.getGatewayIPAddress()); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 341 | dhcpReply.setClientHardwareAddress(dhcpPacket.getClientHardwareAddress()); |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 342 | dhcpReply.setTransactionId(dhcpPacket.getTransactionId()); |
| 343 | |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 344 | if (outgoingMessageType != DHCP.MsgType.DHCPNAK.getValue()) { |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 345 | dhcpReply.setYourIPAddress(ipOffered.toInt()); |
daniel | cd9deed | 2015-10-30 17:16:16 +0900 | [diff] [blame] | 346 | dhcpReply.setServerIPAddress(dhcpServerReply.toInt()); |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 347 | if (dhcpPacket.getGatewayIPAddress() == 0) { |
| 348 | ipv4Reply.setDestinationAddress(IP_BROADCAST.toInt()); |
| 349 | } |
| 350 | } |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 351 | dhcpReply.setHardwareType(DHCP.HWTYPE_ETHERNET); |
| 352 | dhcpReply.setHardwareAddressLength((byte) 6); |
| 353 | |
| 354 | // DHCP Options. |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 355 | DhcpOption option = new DhcpOption(); |
| 356 | List<DhcpOption> optionList = new ArrayList<>(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 357 | |
| 358 | // DHCP Message Type. |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 359 | option.setCode(OptionCode_MessageType.getValue()); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 360 | option.setLength((byte) 1); |
| 361 | byte[] optionData = {outgoingMessageType}; |
| 362 | option.setData(optionData); |
| 363 | optionList.add(option); |
| 364 | |
| 365 | // DHCP Server Identifier. |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 366 | option = new DhcpOption(); |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 367 | option.setCode(OptionCode_DHCPServerIp.getValue()); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 368 | option.setLength((byte) 4); |
daniel | cd9deed | 2015-10-30 17:16:16 +0900 | [diff] [blame] | 369 | option.setData(dhcpServerReply.toOctets()); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 370 | optionList.add(option); |
| 371 | |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 372 | if (outgoingMessageType != DHCP.MsgType.DHCPNAK.getValue()) { |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 373 | // IP Address Lease Time. |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 374 | option = new DhcpOption(); |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 375 | option.setCode(DHCP.DHCPOptionCode.OptionCode_LeaseTime.getValue()); |
| 376 | option.setLength((byte) 4); |
daniel | 02f3af0 | 2015-12-17 18:44:52 +0900 | [diff] [blame] | 377 | option.setData(ByteBuffer.allocate(4) |
| 378 | .putInt(ipAssignment == null ? leaseTime : ipAssignment.leasePeriod()).array()); |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 379 | optionList.add(option); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 380 | |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 381 | // IP Address Renewal Time. |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 382 | option = new DhcpOption(); |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 383 | option.setCode(DHCP.DHCPOptionCode.OptionCode_RenewalTime.getValue()); |
| 384 | option.setLength((byte) 4); |
| 385 | option.setData(ByteBuffer.allocate(4).putInt(renewalTime).array()); |
| 386 | optionList.add(option); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 387 | |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 388 | // IP Address Rebinding Time. |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 389 | option = new DhcpOption(); |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 390 | option.setCode(DHCP.DHCPOptionCode.OPtionCode_RebindingTime.getValue()); |
| 391 | option.setLength((byte) 4); |
| 392 | option.setData(ByteBuffer.allocate(4).putInt(rebindingTime).array()); |
| 393 | optionList.add(option); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 394 | |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 395 | // Subnet Mask. |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 396 | option = new DhcpOption(); |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 397 | option.setCode(DHCP.DHCPOptionCode.OptionCode_SubnetMask.getValue()); |
| 398 | option.setLength((byte) 4); |
daniel | cd9deed | 2015-10-30 17:16:16 +0900 | [diff] [blame] | 399 | option.setData(subnetMaskReply.toOctets()); |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 400 | optionList.add(option); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 401 | |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 402 | // Broadcast Address. |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 403 | option = new DhcpOption(); |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 404 | option.setCode(DHCP.DHCPOptionCode.OptionCode_BroadcastAddress.getValue()); |
| 405 | option.setLength((byte) 4); |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 406 | option.setData(broadcastReply.toOctets()); |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 407 | optionList.add(option); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 408 | |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 409 | // Router Address. |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 410 | if (routerAddressReply.isPresent()) { |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 411 | option = new DhcpOption(); |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 412 | option.setCode(DHCP.DHCPOptionCode.OptionCode_RouterAddress.getValue()); |
| 413 | option.setLength((byte) 4); |
| 414 | option.setData(routerAddressReply.get().toOctets()); |
| 415 | optionList.add(option); |
| 416 | } |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 417 | |
| 418 | // DNS Server Address. |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 419 | if (domainServerReply.isPresent()) { |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 420 | option = new DhcpOption(); |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 421 | option.setCode(DHCP.DHCPOptionCode.OptionCode_DomainServer.getValue()); |
| 422 | option.setLength((byte) 4); |
| 423 | option.setData(domainServerReply.get().toOctets()); |
| 424 | optionList.add(option); |
| 425 | } |
samanwita pal | a3d1d1c | 2015-09-25 11:50:15 -0700 | [diff] [blame] | 426 | } |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 427 | |
| 428 | // End Option. |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 429 | option = new DhcpOption(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 430 | option.setCode(DHCP.DHCPOptionCode.OptionCode_END.getValue()); |
| 431 | option.setLength((byte) 1); |
| 432 | optionList.add(option); |
| 433 | |
| 434 | dhcpReply.setOptions(optionList); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 435 | udpReply.setPayload(dhcpReply); |
| 436 | ipv4Reply.setPayload(udpReply); |
| 437 | ethReply.setPayload(ipv4Reply); |
| 438 | |
| 439 | return ethReply; |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * Sends the Ethernet reply frame via the Packet Service. |
| 444 | * |
| 445 | * @param context the context of the incoming frame |
| 446 | * @param reply the Ethernet reply frame |
| 447 | */ |
| 448 | private void sendReply(PacketContext context, Ethernet reply) { |
| 449 | if (reply != null) { |
| 450 | TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder(); |
| 451 | ConnectPoint sourcePoint = context.inPacket().receivedFrom(); |
| 452 | builder.setOutput(sourcePoint.port()); |
Thomas Vachuska | 0009044 | 2015-09-11 18:08:04 -0700 | [diff] [blame] | 453 | context.block(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 454 | packetService.emit(new DefaultOutboundPacket(sourcePoint.deviceId(), |
samanwita pal | 8969cbe | 2015-09-04 13:31:30 -0700 | [diff] [blame] | 455 | builder.build(), ByteBuffer.wrap(reply.serialize()))); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Processes the DHCP Payload and initiates a reply to the client. |
| 461 | * |
| 462 | * @param context context of the incoming message |
| 463 | * @param dhcpPayload the extracted DHCP payload |
| 464 | */ |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 465 | private void processDhcpPacket(PacketContext context, DHCP dhcpPayload) { |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 466 | if (dhcpPayload == null) { |
| 467 | log.debug("DHCP packet without payload, do nothing"); |
| 468 | return; |
| 469 | } |
| 470 | |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 471 | Ethernet packet = context.inPacket().parsed(); |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 472 | DHCP.MsgType incomingPacketType = null; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 473 | boolean flagIfRequestedIP = false; |
| 474 | boolean flagIfServerIP = false; |
| 475 | Ip4Address requestedIP = Ip4Address.valueOf("0.0.0.0"); |
| 476 | Ip4Address serverIP = Ip4Address.valueOf("0.0.0.0"); |
| 477 | |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 478 | for (DhcpOption option : dhcpPayload.getOptions()) { |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 479 | if (option.getCode() == OptionCode_MessageType.getValue()) { |
| 480 | byte[] data = option.getData(); |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 481 | incomingPacketType = DHCP.MsgType.getType(data[0]); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 482 | } |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 483 | if (option.getCode() == OptionCode_RequestedIP.getValue()) { |
| 484 | byte[] data = option.getData(); |
| 485 | requestedIP = Ip4Address.valueOf(data); |
| 486 | flagIfRequestedIP = true; |
| 487 | } |
| 488 | if (option.getCode() == OptionCode_DHCPServerIp.getValue()) { |
| 489 | byte[] data = option.getData(); |
| 490 | serverIP = Ip4Address.valueOf(data); |
| 491 | flagIfServerIP = true; |
| 492 | } |
| 493 | } |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 494 | |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 495 | if (incomingPacketType == null) { |
| 496 | log.debug("No incoming packet type specified, ignore it"); |
| 497 | return; |
| 498 | } |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 499 | |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 500 | DHCP.MsgType outgoingPacketType; |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 501 | MacAddress clientMac = new MacAddress(dhcpPayload.getClientHardwareAddress()); |
| 502 | VlanId vlanId = VlanId.vlanId(packet.getVlanID()); |
| 503 | HostId hostId = HostId.hostId(clientMac, vlanId); |
daniel | cd9deed | 2015-10-30 17:16:16 +0900 | [diff] [blame] | 504 | |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 505 | switch (incomingPacketType) { |
| 506 | case DHCPDISCOVER: |
| 507 | log.trace("DHCP DISCOVER received from {}", hostId); |
| 508 | Ip4Address ipOffered = dhcpStore.suggestIP(hostId, requestedIP); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 509 | if (ipOffered != null) { |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 510 | Ethernet ethReply = buildReply( |
| 511 | packet, |
| 512 | ipOffered, |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 513 | (byte) DHCP.MsgType.DHCPOFFER.getValue()); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 514 | sendReply(context, ethReply); |
| 515 | } |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 516 | break; |
| 517 | case DHCPREQUEST: |
| 518 | log.trace("DHCP REQUEST received from {}", hostId); |
| 519 | if (flagIfServerIP && !myIP.equals(serverIP)) { |
| 520 | return; |
| 521 | } |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 522 | |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 523 | if (!flagIfRequestedIP) { |
| 524 | // this is renew or rebinding request |
| 525 | int clientIp = dhcpPayload.getClientIPAddress(); |
| 526 | requestedIP = Ip4Address.valueOf(clientIp); |
| 527 | } |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 528 | |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 529 | IpAssignment ipAssignment = IpAssignment.builder() |
| 530 | .ipAddress(requestedIP) |
| 531 | .leasePeriod(leaseTime) |
| 532 | .timestamp(new Date()) |
| 533 | .assignmentStatus(Option_Requested).build(); |
daniel | 877bb2f | 2015-11-12 21:33:05 +0900 | [diff] [blame] | 534 | |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 535 | if (dhcpStore.assignIP(hostId, ipAssignment)) { |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 536 | outgoingPacketType = DHCP.MsgType.DHCPACK; |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 537 | discoverHost(context, requestedIP); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 538 | } else { |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 539 | outgoingPacketType = DHCP.MsgType.DHCPNAK; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 540 | } |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 541 | |
| 542 | Ethernet ethReply = buildReply(packet, requestedIP, (byte) outgoingPacketType.getValue()); |
| 543 | sendReply(context, ethReply); |
| 544 | break; |
| 545 | case DHCPRELEASE: |
| 546 | log.trace("DHCP RELEASE received from {}", hostId); |
| 547 | Ip4Address releaseIp = dhcpStore.releaseIP(hostId); |
| 548 | if (releaseIp != null) { |
| 549 | hostProviderService.removeIpFromHost(hostId, releaseIp); |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 550 | } |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 551 | break; |
| 552 | default: |
| 553 | break; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * Processes the ARP Payload and initiates a reply to the client. |
| 559 | * |
| 560 | * @param context context of the incoming message |
| 561 | * @param packet the ethernet payload |
| 562 | */ |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 563 | private void processArpPacket(PacketContext context, Ethernet packet) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 564 | |
| 565 | ARP arpPacket = (ARP) packet.getPayload(); |
| 566 | |
| 567 | ARP arpReply = (ARP) arpPacket.clone(); |
| 568 | arpReply.setOpCode(ARP.OP_REPLY); |
| 569 | |
| 570 | arpReply.setTargetProtocolAddress(arpPacket.getSenderProtocolAddress()); |
| 571 | arpReply.setTargetHardwareAddress(arpPacket.getSenderHardwareAddress()); |
| 572 | arpReply.setSenderProtocolAddress(arpPacket.getTargetProtocolAddress()); |
| 573 | arpReply.setSenderHardwareAddress(myMAC.toBytes()); |
| 574 | |
| 575 | // Ethernet Frame. |
| 576 | Ethernet ethReply = new Ethernet(); |
| 577 | ethReply.setSourceMACAddress(myMAC); |
| 578 | ethReply.setDestinationMACAddress(packet.getSourceMAC()); |
| 579 | ethReply.setEtherType(Ethernet.TYPE_ARP); |
| 580 | ethReply.setVlanID(packet.getVlanID()); |
| 581 | |
| 582 | ethReply.setPayload(arpReply); |
| 583 | sendReply(context, ethReply); |
| 584 | } |
| 585 | |
| 586 | /** |
| 587 | * Integrates hosts learned through DHCP into topology. |
| 588 | * @param context context of the incoming message |
| 589 | * @param ipAssigned IP Address assigned to the host by DHCP Manager |
| 590 | */ |
| 591 | private void discoverHost(PacketContext context, Ip4Address ipAssigned) { |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 592 | if (!allowHostDiscovery) { |
| 593 | // host discovery is not allowed, do nothing |
| 594 | return; |
| 595 | } |
| 596 | |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 597 | Ethernet packet = context.inPacket().parsed(); |
| 598 | MacAddress mac = packet.getSourceMAC(); |
| 599 | VlanId vlanId = VlanId.vlanId(packet.getVlanID()); |
| 600 | HostLocation hostLocation = new HostLocation(context.inPacket().receivedFrom(), 0); |
| 601 | |
| 602 | Set<IpAddress> ips = new HashSet<>(); |
| 603 | ips.add(ipAssigned); |
| 604 | |
| 605 | HostId hostId = HostId.hostId(mac, vlanId); |
| 606 | DefaultHostDescription desc = new DefaultHostDescription(mac, vlanId, hostLocation, ips); |
Hyunsun Moon | 04b1fe9 | 2016-05-18 21:28:06 -0700 | [diff] [blame] | 607 | |
| 608 | log.info("Discovered host {}", desc); |
Ray Milkey | dc08344 | 2016-02-22 11:27:57 -0800 | [diff] [blame] | 609 | hostProviderService.hostDetected(hostId, desc, false); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | |
| 613 | @Override |
| 614 | public void process(PacketContext context) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 615 | Ethernet packet = context.inPacket().parsed(); |
| 616 | if (packet == null) { |
| 617 | return; |
| 618 | } |
| 619 | |
| 620 | if (packet.getEtherType() == Ethernet.TYPE_IPV4) { |
| 621 | IPv4 ipv4Packet = (IPv4) packet.getPayload(); |
| 622 | |
| 623 | if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_UDP) { |
| 624 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 625 | |
| 626 | if (udpPacket.getDestinationPort() == UDP.DHCP_SERVER_PORT && |
| 627 | udpPacket.getSourcePort() == UDP.DHCP_CLIENT_PORT) { |
| 628 | // This is meant for the dhcp server so process the packet here. |
| 629 | |
| 630 | DHCP dhcpPayload = (DHCP) udpPacket.getPayload(); |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 631 | processDhcpPacket(context, dhcpPayload); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | } else if (packet.getEtherType() == Ethernet.TYPE_ARP) { |
| 635 | ARP arpPacket = (ARP) packet.getPayload(); |
| 636 | |
| 637 | if ((arpPacket.getOpCode() == ARP.OP_REQUEST) && |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 638 | Objects.equals(myIP, Ip4Address.valueOf(arpPacket.getTargetProtocolAddress()))) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 639 | |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 640 | processArpPacket(context, packet); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 641 | |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | private class InternalConfigListener implements NetworkConfigListener { |
| 648 | |
| 649 | /** |
| 650 | * Reconfigures the DHCP Server according to the configuration parameters passed. |
| 651 | * |
| 652 | * @param cfg configuration object |
| 653 | */ |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 654 | private void reconfigureNetwork(DhcpConfig cfg) { |
Thomas Vachuska | 0009044 | 2015-09-11 18:08:04 -0700 | [diff] [blame] | 655 | if (cfg == null) { |
| 656 | return; |
| 657 | } |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 658 | if (cfg.ip() != null) { |
| 659 | myIP = cfg.ip(); |
| 660 | } |
| 661 | if (cfg.mac() != null) { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 662 | myMAC = cfg.mac(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 663 | } |
| 664 | if (cfg.subnetMask() != null) { |
| 665 | subnetMask = cfg.subnetMask(); |
| 666 | } |
| 667 | if (cfg.broadcastAddress() != null) { |
| 668 | broadcastAddress = cfg.broadcastAddress(); |
| 669 | } |
| 670 | if (cfg.routerAddress() != null) { |
| 671 | routerAddress = cfg.routerAddress(); |
| 672 | } |
| 673 | if (cfg.domainServer() != null) { |
| 674 | domainServer = cfg.domainServer(); |
| 675 | } |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 676 | if (cfg.ttl() != -1) { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 677 | packetTTL = (byte) cfg.ttl(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 678 | } |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 679 | if (cfg.leaseTime() != -1) { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 680 | leaseTime = cfg.leaseTime(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 681 | } |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 682 | if (cfg.renewTime() != -1) { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 683 | renewalTime = cfg.renewTime(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 684 | } |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 685 | if (cfg.rebindTime() != -1) { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 686 | rebindingTime = cfg.rebindTime(); |
| 687 | } |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 688 | if (cfg.defaultTimeout() != -1) { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 689 | dhcpStore.setDefaultTimeoutForPurge(cfg.defaultTimeout()); |
| 690 | } |
samanwita pal | 7ccc2bc | 2015-09-14 19:53:15 -0700 | [diff] [blame] | 691 | if (cfg.timerDelay() != -1) { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 692 | timerDelay = cfg.timerDelay(); |
| 693 | } |
| 694 | if ((cfg.startIp() != null) && (cfg.endIp() != null)) { |
| 695 | dhcpStore.populateIPPoolfromRange(cfg.startIp(), cfg.endIp()); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 699 | |
| 700 | @Override |
| 701 | public void event(NetworkConfigEvent event) { |
| 702 | |
| 703 | if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 704 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) && |
| 705 | event.configClass().equals(DhcpConfig.class)) { |
| 706 | |
| 707 | DhcpConfig cfg = cfgService.getConfig(appId, DhcpConfig.class); |
| 708 | reconfigureNetwork(cfg); |
| 709 | log.info("Reconfigured"); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | private class InternalHostProvider extends AbstractProvider implements HostProvider { |
| 715 | |
| 716 | /** |
| 717 | * Creates a provider with the supplier identifier. |
| 718 | */ |
| 719 | protected InternalHostProvider() { |
| 720 | super(PID); |
| 721 | } |
| 722 | |
| 723 | @Override |
| 724 | public void triggerProbe(Host host) { |
| 725 | // nothing to do |
| 726 | } |
| 727 | } |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 728 | |
Yuta HIGUCHI | 19afc03 | 2017-05-20 23:44:17 -0700 | [diff] [blame] | 729 | private class PurgeListTask implements Runnable { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 730 | |
| 731 | @Override |
Yuta HIGUCHI | 19afc03 | 2017-05-20 23:44:17 -0700 | [diff] [blame] | 732 | public void run() { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 733 | IpAssignment ipAssignment; |
| 734 | Date dateNow = new Date(); |
| 735 | |
samanwita pal | 0bff430 | 2015-09-15 13:37:00 -0700 | [diff] [blame] | 736 | Map<HostId, IpAssignment> ipAssignmentMap = dhcpStore.listAllMapping(); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 737 | for (Map.Entry<HostId, IpAssignment> entry: ipAssignmentMap.entrySet()) { |
| 738 | ipAssignment = entry.getValue(); |
| 739 | |
| 740 | long timeLapsed = dateNow.getTime() - ipAssignment.timestamp().getTime(); |
| 741 | if ((ipAssignment.assignmentStatus() != IpAssignment.AssignmentStatus.Option_Expired) && |
| 742 | (ipAssignment.leasePeriod() > 0) && (timeLapsed > (ipAssignment.leasePeriodMs()))) { |
| 743 | |
samanwita pal | c40e5ed | 2015-09-24 11:01:51 -0700 | [diff] [blame] | 744 | Ip4Address ip4Address = dhcpStore.releaseIP(entry.getKey()); |
| 745 | if (ip4Address != null) { |
| 746 | hostProviderService.removeIpFromHost(entry.getKey(), ipAssignment.ipAddress()); |
| 747 | } |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 748 | } |
| 749 | } |
Yuta HIGUCHI | 19afc03 | 2017-05-20 23:44:17 -0700 | [diff] [blame] | 750 | timeout = SharedScheduledExecutors.newTimeout(new PurgeListTask(), timerDelay, TimeUnit.MINUTES); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 751 | } |
| 752 | } |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 753 | } |