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