Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | */ |
| 17 | |
| 18 | package org.onosproject.dhcprelay; |
| 19 | |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 20 | import com.google.common.base.MoreObjects; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Sets; |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Component; |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Deactivate; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Property; |
| 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.BasePacket; |
| 30 | import org.onlab.packet.DHCP; |
| 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.UDP; |
| 37 | import org.onlab.packet.VlanId; |
| 38 | import org.onlab.packet.dhcp.CircuitId; |
| 39 | import org.onlab.packet.dhcp.DhcpOption; |
| 40 | import org.onlab.packet.dhcp.DhcpRelayAgentOption; |
| 41 | import org.onosproject.dhcprelay.api.DhcpHandler; |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 42 | import org.onosproject.dhcprelay.config.DhcpServerConfig; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 43 | import org.onosproject.dhcprelay.store.DhcpRecord; |
| 44 | import org.onosproject.dhcprelay.store.DhcpRelayStore; |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 45 | import org.onosproject.net.host.HostEvent; |
| 46 | import org.onosproject.net.host.HostListener; |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 47 | import org.onosproject.net.host.HostProvider; |
| 48 | import org.onosproject.net.host.HostProviderRegistry; |
| 49 | import org.onosproject.net.host.HostProviderService; |
Ray Milkey | facf286 | 2017-08-03 11:58:29 -0700 | [diff] [blame] | 50 | import org.onosproject.net.intf.Interface; |
| 51 | import org.onosproject.net.intf.InterfaceService; |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 52 | import org.onosproject.net.provider.ProviderId; |
Ray Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 53 | import org.onosproject.routeservice.Route; |
| 54 | import org.onosproject.routeservice.RouteStore; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 55 | import org.onosproject.net.ConnectPoint; |
| 56 | import org.onosproject.net.Host; |
| 57 | import org.onosproject.net.HostId; |
| 58 | import org.onosproject.net.HostLocation; |
| 59 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 60 | import org.onosproject.net.flow.TrafficTreatment; |
| 61 | import org.onosproject.net.host.DefaultHostDescription; |
| 62 | import org.onosproject.net.host.HostDescription; |
| 63 | import org.onosproject.net.host.HostService; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 64 | import org.onosproject.net.host.InterfaceIpAddress; |
| 65 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 66 | import org.onosproject.net.packet.OutboundPacket; |
| 67 | import org.onosproject.net.packet.PacketContext; |
| 68 | import org.onosproject.net.packet.PacketService; |
| 69 | import org.slf4j.Logger; |
| 70 | import org.slf4j.LoggerFactory; |
| 71 | |
| 72 | import java.nio.ByteBuffer; |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 73 | import java.util.Collection; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 74 | import java.util.Collections; |
| 75 | import java.util.List; |
| 76 | import java.util.Optional; |
| 77 | import java.util.Set; |
| 78 | import java.util.stream.Collectors; |
| 79 | |
| 80 | import static com.google.common.base.Preconditions.checkNotNull; |
| 81 | import static com.google.common.base.Preconditions.checkState; |
| 82 | import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_CircuitID; |
| 83 | import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_END; |
| 84 | import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_MessageType; |
| 85 | import static org.onlab.packet.MacAddress.valueOf; |
| 86 | import static org.onlab.packet.dhcp.DhcpRelayAgentOption.RelayAgentInfoOptions.CIRCUIT_ID; |
| 87 | |
| 88 | @Component |
| 89 | @Service |
| 90 | @Property(name = "version", value = "4") |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 91 | public class Dhcp4HandlerImpl implements DhcpHandler, HostProvider { |
Charles Chan | d988c28 | 2017-09-12 17:09:32 -0700 | [diff] [blame] | 92 | public static final String DHCP_V4_RELAY_APP = "org.onosproject.Dhcp4HandlerImpl"; |
| 93 | public static final ProviderId PROVIDER_ID = new ProviderId("dhcp4", DHCP_V4_RELAY_APP); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 94 | private static Logger log = LoggerFactory.getLogger(Dhcp4HandlerImpl.class); |
| 95 | |
| 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 97 | protected DhcpRelayStore dhcpRelayStore; |
| 98 | |
| 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 100 | protected PacketService packetService; |
| 101 | |
| 102 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 103 | protected RouteStore routeStore; |
| 104 | |
| 105 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 106 | protected InterfaceService interfaceService; |
| 107 | |
| 108 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 109 | protected HostService hostService; |
| 110 | |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 111 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 112 | protected HostProviderRegistry providerRegistry; |
| 113 | |
| 114 | protected HostProviderService providerService; |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 115 | private InternalHostListener hostListener = new InternalHostListener(); |
| 116 | |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 117 | private Ip4Address dhcpServerIp = null; |
| 118 | // dhcp server may be connected directly to the SDN network or |
| 119 | // via an external gateway. When connected directly, the dhcpConnectPoint, dhcpConnectMac, |
| 120 | // and dhcpConnectVlan refer to the server. When connected via the gateway, they refer |
| 121 | // to the gateway. |
| 122 | private ConnectPoint dhcpServerConnectPoint = null; |
| 123 | private MacAddress dhcpConnectMac = null; |
| 124 | private VlanId dhcpConnectVlan = null; |
| 125 | private Ip4Address dhcpGatewayIp = null; |
Yi Tseng | 3df7f9d | 2017-08-17 13:08:31 -0700 | [diff] [blame] | 126 | private Ip4Address relayAgentIp = null; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 127 | |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 128 | // Indirect case DHCP server |
| 129 | private Ip4Address indirectDhcpServerIp = null; |
| 130 | private ConnectPoint indirectDhcpServerConnectPoint = null; |
| 131 | private MacAddress indirectDhcpConnectMac = null; |
| 132 | private VlanId indirectDhcpConnectVlan = null; |
| 133 | private Ip4Address indirectDhcpGatewayIp = null; |
| 134 | private Ip4Address indirectRelayAgentIp = null; |
| 135 | |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 136 | @Activate |
| 137 | protected void activate() { |
| 138 | hostService.addListener(hostListener); |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 139 | providerService = providerRegistry.register(this); |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Deactivate |
| 143 | protected void deactivate() { |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 144 | providerRegistry.unregister(this); |
| 145 | hostService.removeListener(hostListener); |
| 146 | this.dhcpConnectMac = null; |
| 147 | this.dhcpConnectVlan = null; |
| 148 | |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 149 | if (dhcpGatewayIp != null) { |
| 150 | hostService.stopMonitoringIp(dhcpGatewayIp); |
| 151 | } |
| 152 | if (dhcpServerIp != null) { |
| 153 | hostService.stopMonitoringIp(dhcpServerIp); |
| 154 | } |
| 155 | |
| 156 | if (indirectDhcpGatewayIp != null) { |
| 157 | hostService.stopMonitoringIp(indirectDhcpGatewayIp); |
| 158 | } |
| 159 | if (indirectDhcpServerIp != null) { |
| 160 | hostService.stopMonitoringIp(indirectDhcpServerIp); |
| 161 | } |
| 162 | |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 163 | hostService.removeListener(hostListener); |
| 164 | this.dhcpConnectMac = null; |
| 165 | this.dhcpConnectVlan = null; |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 166 | this.indirectDhcpConnectMac = null; |
| 167 | this.indirectDhcpConnectVlan = null; |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 170 | @Override |
| 171 | public void setDhcpServerIp(IpAddress dhcpServerIp) { |
| 172 | checkNotNull(dhcpServerIp, "DHCP server IP can't be null"); |
| 173 | checkState(dhcpServerIp.isIp4(), "Invalid server IP for DHCPv4 relay handler"); |
| 174 | this.dhcpServerIp = dhcpServerIp.getIp4Address(); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public void setDhcpServerConnectPoint(ConnectPoint dhcpServerConnectPoint) { |
| 179 | checkNotNull(dhcpServerConnectPoint, "Server connect point can't null"); |
| 180 | this.dhcpServerConnectPoint = dhcpServerConnectPoint; |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public void setDhcpConnectMac(MacAddress dhcpConnectMac) { |
| 185 | this.dhcpConnectMac = dhcpConnectMac; |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public void setDhcpConnectVlan(VlanId dhcpConnectVlan) { |
| 190 | this.dhcpConnectVlan = dhcpConnectVlan; |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public void setDhcpGatewayIp(IpAddress dhcpGatewayIp) { |
| 195 | if (dhcpGatewayIp != null) { |
| 196 | checkState(dhcpGatewayIp.isIp4(), "Invalid gateway IP for DHCPv4 relay handler"); |
| 197 | this.dhcpGatewayIp = dhcpGatewayIp.getIp4Address(); |
| 198 | } else { |
| 199 | // removes gateway config |
| 200 | this.dhcpGatewayIp = null; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public Optional<IpAddress> getDhcpServerIp() { |
| 206 | return Optional.ofNullable(dhcpServerIp); |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public Optional<IpAddress> getDhcpGatewayIp() { |
| 211 | return Optional.ofNullable(dhcpGatewayIp); |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public Optional<MacAddress> getDhcpConnectMac() { |
| 216 | return Optional.ofNullable(dhcpConnectMac); |
| 217 | } |
| 218 | |
| 219 | @Override |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 220 | public void setDefaultDhcpServerConfigs(Collection<DhcpServerConfig> configs) { |
| 221 | if (configs.size() == 0) { |
| 222 | // no config to update |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | // TODO: currently we pick up first DHCP server config. |
| 227 | // Will use other server configs in the future for HA. |
| 228 | DhcpServerConfig serverConfig = configs.iterator().next(); |
Yi Tseng | a5cde45 | 2017-09-08 16:23:32 -0700 | [diff] [blame] | 229 | if (!serverConfig.getDhcpServerConnectPoint().isPresent()) { |
| 230 | log.warn("Connect point from server config not exists"); |
| 231 | return; |
| 232 | } |
| 233 | if (!serverConfig.getDhcpServerIp4().isPresent()) { |
| 234 | log.warn("IP from DHCP server config not exists"); |
| 235 | return; |
| 236 | } |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 237 | Ip4Address oldServerIp = this.dhcpServerIp; |
| 238 | Ip4Address oldGatewayIp = this.dhcpGatewayIp; |
| 239 | |
| 240 | // stop monitoring gateway or server |
| 241 | if (oldGatewayIp != null) { |
| 242 | hostService.stopMonitoringIp(oldGatewayIp); |
| 243 | } else if (oldServerIp != null) { |
| 244 | hostService.stopMonitoringIp(oldServerIp); |
| 245 | } |
| 246 | |
| 247 | this.dhcpServerConnectPoint = serverConfig.getDhcpServerConnectPoint().get(); |
| 248 | this.dhcpServerIp = serverConfig.getDhcpServerIp4().get(); |
| 249 | this.dhcpGatewayIp = serverConfig.getDhcpGatewayIp4().orElse(null); |
| 250 | |
| 251 | // reset server mac and vlan |
| 252 | this.dhcpConnectMac = null; |
| 253 | this.dhcpConnectVlan = null; |
| 254 | |
| 255 | log.info("DHCP server connect point: " + this.dhcpServerConnectPoint); |
| 256 | log.info("DHCP server IP: " + this.dhcpServerIp); |
| 257 | |
| 258 | IpAddress ipToProbe = MoreObjects.firstNonNull(this.dhcpGatewayIp, this.dhcpServerIp); |
| 259 | String hostToProbe = this.dhcpGatewayIp != null ? "gateway" : "DHCP server"; |
| 260 | |
| 261 | if (ipToProbe == null) { |
| 262 | log.warn("Server IP not set, can't probe it"); |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | log.info("Probing to resolve {} IP {}", hostToProbe, ipToProbe); |
| 267 | hostService.startMonitoringIp(ipToProbe); |
| 268 | |
| 269 | Set<Host> hosts = hostService.getHostsByIp(ipToProbe); |
| 270 | if (!hosts.isEmpty()) { |
| 271 | Host host = hosts.iterator().next(); |
| 272 | this.dhcpConnectVlan = host.vlan(); |
| 273 | this.dhcpConnectMac = host.mac(); |
| 274 | } |
Yi Tseng | 3df7f9d | 2017-08-17 13:08:31 -0700 | [diff] [blame] | 275 | |
| 276 | this.relayAgentIp = serverConfig.getRelayAgentIp4().orElse(null); |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public void setIndirectDhcpServerConfigs(Collection<DhcpServerConfig> configs) { |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 281 | if (configs.size() == 0) { |
| 282 | // no config to update |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | // TODO: currently we pick up first indirect DHCP server config. |
| 287 | // Will use other server configs in the future for HA. |
| 288 | DhcpServerConfig serverConfig = configs.iterator().next(); |
| 289 | checkState(serverConfig.getDhcpServerConnectPoint().isPresent(), |
| 290 | "Connect point not exists"); |
| 291 | checkState(serverConfig.getDhcpServerIp4().isPresent(), |
| 292 | "IP of DHCP server not exists"); |
| 293 | Ip4Address oldServerIp = this.indirectDhcpServerIp; |
| 294 | Ip4Address oldGatewayIp = this.indirectDhcpGatewayIp; |
| 295 | |
| 296 | // stop monitoring gateway or server |
| 297 | if (oldGatewayIp != null) { |
| 298 | hostService.stopMonitoringIp(oldGatewayIp); |
| 299 | } else if (oldServerIp != null) { |
| 300 | hostService.stopMonitoringIp(oldServerIp); |
| 301 | } |
| 302 | |
| 303 | this.indirectDhcpServerConnectPoint = serverConfig.getDhcpServerConnectPoint().get(); |
| 304 | this.indirectDhcpServerIp = serverConfig.getDhcpServerIp4().get(); |
| 305 | this.indirectDhcpGatewayIp = serverConfig.getDhcpGatewayIp4().orElse(null); |
| 306 | |
| 307 | // reset server mac and vlan |
| 308 | this.indirectDhcpConnectMac = null; |
| 309 | this.indirectDhcpConnectVlan = null; |
| 310 | |
| 311 | log.info("Indirect DHCP server connect point: " + this.indirectDhcpServerConnectPoint); |
| 312 | log.info("Indirect DHCP server IP: " + this.indirectDhcpServerIp); |
| 313 | |
| 314 | IpAddress ipToProbe = MoreObjects.firstNonNull(this.indirectDhcpGatewayIp, this.indirectDhcpServerIp); |
| 315 | String hostToProbe = this.indirectDhcpGatewayIp != null ? "gateway" : "DHCP server"; |
| 316 | |
| 317 | log.info("Probing to resolve {} IP {}", hostToProbe, ipToProbe); |
| 318 | hostService.startMonitoringIp(ipToProbe); |
| 319 | |
| 320 | Set<Host> hosts = hostService.getHostsByIp(ipToProbe); |
| 321 | if (!hosts.isEmpty()) { |
| 322 | Host host = hosts.iterator().next(); |
| 323 | this.indirectDhcpConnectVlan = host.vlan(); |
| 324 | this.indirectDhcpConnectMac = host.mac(); |
| 325 | } |
| 326 | |
| 327 | this.indirectRelayAgentIp = serverConfig.getRelayAgentIp4().orElse(null); |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Yi Tseng | 3df7f9d | 2017-08-17 13:08:31 -0700 | [diff] [blame] | 330 | @Override |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 331 | public void processDhcpPacket(PacketContext context, BasePacket payload) { |
| 332 | checkNotNull(payload, "DHCP payload can't be null"); |
| 333 | checkState(payload instanceof DHCP, "Payload is not a DHCP"); |
| 334 | DHCP dhcpPayload = (DHCP) payload; |
| 335 | if (!configured()) { |
| 336 | log.warn("Missing DHCP relay server config. Abort packet processing"); |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | ConnectPoint inPort = context.inPacket().receivedFrom(); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 341 | checkNotNull(dhcpPayload, "Can't find DHCP payload"); |
| 342 | Ethernet packet = context.inPacket().parsed(); |
| 343 | DHCP.MsgType incomingPacketType = dhcpPayload.getOptions().stream() |
| 344 | .filter(dhcpOption -> dhcpOption.getCode() == OptionCode_MessageType.getValue()) |
| 345 | .map(DhcpOption::getData) |
| 346 | .map(data -> DHCP.MsgType.getType(data[0])) |
| 347 | .findFirst() |
| 348 | .orElse(null); |
| 349 | checkNotNull(incomingPacketType, "Can't get message type from DHCP payload {}", dhcpPayload); |
| 350 | switch (incomingPacketType) { |
| 351 | case DHCPDISCOVER: |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 352 | // Add the gateway IP as virtual interface IP for server to understand |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 353 | // the lease to be assigned and forward the packet to dhcp server. |
| 354 | Ethernet ethernetPacketDiscover = |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 355 | processDhcpPacketFromClient(context, packet); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 356 | if (ethernetPacketDiscover != null) { |
| 357 | writeRequestDhcpRecord(inPort, packet, dhcpPayload); |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 358 | handleDhcpDiscoverAndRequest(ethernetPacketDiscover, dhcpPayload); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 359 | } |
| 360 | break; |
| 361 | case DHCPOFFER: |
| 362 | //reply to dhcp client. |
| 363 | Ethernet ethernetPacketOffer = processDhcpPacketFromServer(packet); |
| 364 | if (ethernetPacketOffer != null) { |
| 365 | writeResponseDhcpRecord(ethernetPacketOffer, dhcpPayload); |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 366 | sendResponseToClient(ethernetPacketOffer, dhcpPayload); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 367 | } |
| 368 | break; |
| 369 | case DHCPREQUEST: |
| 370 | // add the gateway ip as virtual interface ip for server to understand |
| 371 | // the lease to be assigned and forward the packet to dhcp server. |
| 372 | Ethernet ethernetPacketRequest = |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 373 | processDhcpPacketFromClient(context, packet); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 374 | if (ethernetPacketRequest != null) { |
| 375 | writeRequestDhcpRecord(inPort, packet, dhcpPayload); |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 376 | handleDhcpDiscoverAndRequest(ethernetPacketRequest, dhcpPayload); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 377 | } |
| 378 | break; |
| 379 | case DHCPACK: |
| 380 | // reply to dhcp client. |
| 381 | Ethernet ethernetPacketAck = processDhcpPacketFromServer(packet); |
| 382 | if (ethernetPacketAck != null) { |
| 383 | writeResponseDhcpRecord(ethernetPacketAck, dhcpPayload); |
| 384 | handleDhcpAck(ethernetPacketAck, dhcpPayload); |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 385 | sendResponseToClient(ethernetPacketAck, dhcpPayload); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 386 | } |
| 387 | break; |
| 388 | case DHCPRELEASE: |
| 389 | // TODO: release the ip address from client |
| 390 | break; |
| 391 | default: |
| 392 | break; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Checks if this app has been configured. |
| 398 | * |
| 399 | * @return true if all information we need have been initialized |
| 400 | */ |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 401 | private boolean configured() { |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 402 | return dhcpServerConnectPoint != null && dhcpServerIp != null; |
| 403 | } |
| 404 | |
| 405 | /** |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 406 | * Returns the first interface ip from interface. |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 407 | * |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 408 | * @param iface interface of one connect point |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 409 | * @return the first interface IP; null if not exists an IP address in |
| 410 | * these interfaces |
| 411 | */ |
Yi Tseng | 3df7f9d | 2017-08-17 13:08:31 -0700 | [diff] [blame] | 412 | private Ip4Address getFirstIpFromInterface(Interface iface) { |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 413 | checkNotNull(iface, "Interface can't be null"); |
| 414 | return iface.ipAddressesList().stream() |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 415 | .map(InterfaceIpAddress::ipAddress) |
| 416 | .filter(IpAddress::isIp4) |
| 417 | .map(IpAddress::getIp4Address) |
| 418 | .findFirst() |
| 419 | .orElse(null); |
| 420 | } |
| 421 | |
| 422 | /** |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 423 | * Gets Interface facing to the server for default host. |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 424 | * |
| 425 | * @return the Interface facing to the server; null if not found |
| 426 | */ |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 427 | private Interface getServerInterface() { |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 428 | if (dhcpServerConnectPoint == null || dhcpConnectVlan == null) { |
| 429 | return null; |
| 430 | } |
| 431 | return interfaceService.getInterfacesByPort(dhcpServerConnectPoint) |
| 432 | .stream() |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 433 | .filter(iface -> interfaceContainsVlan(iface, dhcpConnectVlan)) |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 434 | .findFirst() |
| 435 | .orElse(null); |
| 436 | } |
| 437 | |
| 438 | /** |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 439 | * Gets Interface facing to the server for indirect hosts. |
| 440 | * Use default server Interface if indirect server not configured. |
| 441 | * |
| 442 | * @return the Interface facing to the server; null if not found |
| 443 | */ |
| 444 | private Interface getIndirectServerInterface() { |
| 445 | if (indirectDhcpServerConnectPoint == null || indirectDhcpConnectVlan == null) { |
| 446 | return getServerInterface(); |
| 447 | } |
| 448 | return interfaceService.getInterfacesByPort(indirectDhcpServerConnectPoint) |
| 449 | .stream() |
| 450 | .filter(iface -> interfaceContainsVlan(iface, indirectDhcpConnectVlan)) |
| 451 | .findFirst() |
| 452 | .orElse(null); |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * Determind if an Interface contains a vlan id. |
| 457 | * |
| 458 | * @param iface the Interface |
| 459 | * @param vlanId the vlan id |
| 460 | * @return true if the Interface contains the vlan id |
| 461 | */ |
| 462 | private boolean interfaceContainsVlan(Interface iface, VlanId vlanId) { |
| 463 | return iface.vlan().equals(vlanId) || |
| 464 | iface.vlanUntagged().equals(vlanId) || |
| 465 | iface.vlanTagged().contains(vlanId) || |
| 466 | iface.vlanNative().equals(vlanId); |
| 467 | } |
| 468 | |
| 469 | /** |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 470 | * Build the DHCP discover/request packet with gateway IP(unicast packet). |
| 471 | * |
| 472 | * @param context the packet context |
| 473 | * @param ethernetPacket the ethernet payload to process |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 474 | * @return processed packet |
| 475 | */ |
| 476 | private Ethernet processDhcpPacketFromClient(PacketContext context, |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 477 | Ethernet ethernetPacket) { |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 478 | // get dhcp header. |
| 479 | Ethernet etherReply = (Ethernet) ethernetPacket.clone(); |
| 480 | IPv4 ipv4Packet = (IPv4) etherReply.getPayload(); |
| 481 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 482 | DHCP dhcpPacket = (DHCP) udpPacket.getPayload(); |
| 483 | |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 484 | Ip4Address clientInterfaceIp = |
| 485 | interfaceService.getInterfacesByPort(context.inPacket().receivedFrom()) |
| 486 | .stream() |
| 487 | .map(Interface::ipAddressesList) |
| 488 | .flatMap(Collection::stream) |
| 489 | .map(InterfaceIpAddress::ipAddress) |
| 490 | .filter(IpAddress::isIp4) |
| 491 | .map(IpAddress::getIp4Address) |
| 492 | .findFirst() |
| 493 | .orElse(null); |
| 494 | if (clientInterfaceIp == null) { |
| 495 | log.warn("Can't find interface IP for client interface for port {}", |
| 496 | context.inPacket().receivedFrom()); |
| 497 | return null; |
| 498 | } |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 499 | boolean isDirectlyConnected = directlyConnected(dhcpPacket); |
| 500 | Interface serverInterface = isDirectlyConnected ? getServerInterface() : getIndirectServerInterface(); |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 501 | if (serverInterface == null) { |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 502 | log.warn("Can't get {} server interface, ignore", isDirectlyConnected ? "direct" : "indirect"); |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 503 | return null; |
| 504 | } |
Yi Tseng | 3df7f9d | 2017-08-17 13:08:31 -0700 | [diff] [blame] | 505 | Ip4Address ipFacingServer = getFirstIpFromInterface(serverInterface); |
| 506 | MacAddress macFacingServer = serverInterface.mac(); |
| 507 | if (ipFacingServer == null || macFacingServer == null) { |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 508 | log.warn("No IP address for server Interface {}", serverInterface); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 509 | return null; |
| 510 | } |
| 511 | if (dhcpConnectMac == null) { |
| 512 | log.warn("DHCP {} not yet resolved .. Aborting DHCP " |
| 513 | + "packet processing from client on port: {}", |
| 514 | (dhcpGatewayIp == null) ? "server IP " + dhcpServerIp |
| 515 | : "gateway IP " + dhcpGatewayIp, |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 516 | context.inPacket().receivedFrom()); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 517 | return null; |
| 518 | } |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 519 | |
Yi Tseng | 3df7f9d | 2017-08-17 13:08:31 -0700 | [diff] [blame] | 520 | etherReply.setSourceMACAddress(macFacingServer); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 521 | etherReply.setDestinationMACAddress(dhcpConnectMac); |
| 522 | etherReply.setVlanID(dhcpConnectVlan.toShort()); |
Yi Tseng | 3df7f9d | 2017-08-17 13:08:31 -0700 | [diff] [blame] | 523 | ipv4Packet.setSourceAddress(ipFacingServer.toInt()); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 524 | ipv4Packet.setDestinationAddress(dhcpServerIp.toInt()); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 525 | |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 526 | if (isDirectlyConnected) { |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 527 | ConnectPoint inPort = context.inPacket().receivedFrom(); |
| 528 | VlanId vlanId = VlanId.vlanId(ethernetPacket.getVlanID()); |
| 529 | // add connected in port and vlan |
| 530 | CircuitId cid = new CircuitId(inPort.toString(), vlanId); |
| 531 | byte[] circuitId = cid.serialize(); |
| 532 | DhcpOption circuitIdSubOpt = new DhcpOption(); |
| 533 | circuitIdSubOpt |
| 534 | .setCode(CIRCUIT_ID.getValue()) |
| 535 | .setLength((byte) circuitId.length) |
| 536 | .setData(circuitId); |
| 537 | |
| 538 | DhcpRelayAgentOption newRelayAgentOpt = new DhcpRelayAgentOption(); |
| 539 | newRelayAgentOpt.setCode(OptionCode_CircuitID.getValue()); |
| 540 | newRelayAgentOpt.addSubOption(circuitIdSubOpt); |
| 541 | |
| 542 | // Removes END option first |
| 543 | List<DhcpOption> options = dhcpPacket.getOptions().stream() |
| 544 | .filter(opt -> opt.getCode() != OptionCode_END.getValue()) |
| 545 | .collect(Collectors.toList()); |
| 546 | |
| 547 | // push relay agent option |
| 548 | options.add(newRelayAgentOpt); |
| 549 | |
| 550 | // make sure option 255(End) is the last option |
| 551 | DhcpOption endOption = new DhcpOption(); |
| 552 | endOption.setCode(OptionCode_END.getValue()); |
| 553 | options.add(endOption); |
| 554 | |
| 555 | dhcpPacket.setOptions(options); |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 556 | |
| 557 | // Sets giaddr to IP address from the Interface which facing to |
| 558 | // DHCP client |
| 559 | dhcpPacket.setGatewayIPAddress(clientInterfaceIp.toInt()); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 560 | |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 561 | // replace giaddr if relay agent IP is set |
| 562 | if (relayAgentIp != null) { |
| 563 | dhcpPacket.setGatewayIPAddress(relayAgentIp.toInt()); |
| 564 | } |
| 565 | } else if (indirectDhcpServerIp != null) { |
| 566 | // Indirect case, replace destination to indirect dhcp server if exist |
| 567 | etherReply.setDestinationMACAddress(indirectDhcpConnectMac); |
| 568 | etherReply.setVlanID(indirectDhcpConnectVlan.toShort()); |
| 569 | ipv4Packet.setDestinationAddress(indirectDhcpServerIp.toInt()); |
| 570 | |
| 571 | // replace giaddr if relay agent IP is set |
| 572 | if (indirectRelayAgentIp != null) { |
| 573 | dhcpPacket.setGatewayIPAddress(relayAgentIp.toInt()); |
| 574 | } |
Yi Tseng | 3df7f9d | 2017-08-17 13:08:31 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 577 | udpPacket.setPayload(dhcpPacket); |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 578 | // As a DHCP relay, the source port should be server port( instead |
| 579 | // of client port. |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 580 | udpPacket.setSourcePort(UDP.DHCP_SERVER_PORT); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 581 | udpPacket.setDestinationPort(UDP.DHCP_SERVER_PORT); |
| 582 | ipv4Packet.setPayload(udpPacket); |
| 583 | etherReply.setPayload(ipv4Packet); |
| 584 | return etherReply; |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * Writes DHCP record to the store according to the request DHCP packet (Discover, Request). |
| 589 | * |
| 590 | * @param location the location which DHCP packet comes from |
| 591 | * @param ethernet the DHCP packet |
| 592 | * @param dhcpPayload the DHCP payload |
| 593 | */ |
| 594 | private void writeRequestDhcpRecord(ConnectPoint location, |
| 595 | Ethernet ethernet, |
| 596 | DHCP dhcpPayload) { |
| 597 | VlanId vlanId = VlanId.vlanId(ethernet.getVlanID()); |
| 598 | MacAddress macAddress = MacAddress.valueOf(dhcpPayload.getClientHardwareAddress()); |
| 599 | HostId hostId = HostId.hostId(macAddress, vlanId); |
| 600 | DhcpRecord record = dhcpRelayStore.getDhcpRecord(hostId).orElse(null); |
| 601 | if (record == null) { |
| 602 | record = new DhcpRecord(HostId.hostId(macAddress, vlanId)); |
| 603 | } else { |
| 604 | record = record.clone(); |
| 605 | } |
| 606 | record.addLocation(new HostLocation(location, System.currentTimeMillis())); |
| 607 | record.ip4Status(dhcpPayload.getPacketType()); |
| 608 | record.setDirectlyConnected(directlyConnected(dhcpPayload)); |
| 609 | if (!directlyConnected(dhcpPayload)) { |
| 610 | // Update gateway mac address if the host is not directly connected |
| 611 | record.nextHop(ethernet.getSourceMAC()); |
| 612 | } |
| 613 | record.updateLastSeen(); |
| 614 | dhcpRelayStore.updateDhcpRecord(HostId.hostId(macAddress, vlanId), record); |
| 615 | } |
| 616 | |
| 617 | /** |
| 618 | * Writes DHCP record to the store according to the response DHCP packet (Offer, Ack). |
| 619 | * |
| 620 | * @param ethernet the DHCP packet |
| 621 | * @param dhcpPayload the DHCP payload |
| 622 | */ |
| 623 | private void writeResponseDhcpRecord(Ethernet ethernet, |
| 624 | DHCP dhcpPayload) { |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 625 | Optional<Interface> outInterface = getClientInterface(ethernet, dhcpPayload); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 626 | if (!outInterface.isPresent()) { |
| 627 | log.warn("Failed to determine where to send {}", dhcpPayload.getPacketType()); |
| 628 | return; |
| 629 | } |
| 630 | |
| 631 | Interface outIface = outInterface.get(); |
| 632 | ConnectPoint location = outIface.connectPoint(); |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 633 | VlanId vlanId = getVlanIdFromRelayAgentOption(dhcpPayload); |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 634 | if (vlanId == null) { |
| 635 | vlanId = outIface.vlan(); |
| 636 | } |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 637 | MacAddress macAddress = MacAddress.valueOf(dhcpPayload.getClientHardwareAddress()); |
| 638 | HostId hostId = HostId.hostId(macAddress, vlanId); |
| 639 | DhcpRecord record = dhcpRelayStore.getDhcpRecord(hostId).orElse(null); |
| 640 | if (record == null) { |
| 641 | record = new DhcpRecord(HostId.hostId(macAddress, vlanId)); |
| 642 | } else { |
| 643 | record = record.clone(); |
| 644 | } |
| 645 | record.addLocation(new HostLocation(location, System.currentTimeMillis())); |
| 646 | if (dhcpPayload.getPacketType() == DHCP.MsgType.DHCPACK) { |
| 647 | record.ip4Address(Ip4Address.valueOf(dhcpPayload.getYourIPAddress())); |
| 648 | } |
| 649 | record.ip4Status(dhcpPayload.getPacketType()); |
| 650 | record.setDirectlyConnected(directlyConnected(dhcpPayload)); |
| 651 | record.updateLastSeen(); |
| 652 | dhcpRelayStore.updateDhcpRecord(HostId.hostId(macAddress, vlanId), record); |
| 653 | } |
| 654 | |
| 655 | /** |
| 656 | * Build the DHCP offer/ack with proper client port. |
| 657 | * |
| 658 | * @param ethernetPacket the original packet comes from server |
| 659 | * @return new packet which will send to the client |
| 660 | */ |
| 661 | private Ethernet processDhcpPacketFromServer(Ethernet ethernetPacket) { |
| 662 | // get dhcp header. |
| 663 | Ethernet etherReply = (Ethernet) ethernetPacket.clone(); |
| 664 | IPv4 ipv4Packet = (IPv4) etherReply.getPayload(); |
| 665 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 666 | DHCP dhcpPayload = (DHCP) udpPacket.getPayload(); |
| 667 | |
| 668 | // determine the vlanId of the client host - note that this vlan id |
| 669 | // could be different from the vlan in the packet from the server |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 670 | Interface clientInterface = getClientInterface(ethernetPacket, dhcpPayload).orElse(null); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 671 | |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 672 | if (clientInterface == null) { |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 673 | log.warn("Cannot find the interface for the DHCP {}", dhcpPayload); |
| 674 | return null; |
| 675 | } |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 676 | VlanId vlanId; |
| 677 | if (clientInterface.vlanTagged().isEmpty()) { |
| 678 | vlanId = clientInterface.vlan(); |
| 679 | } else { |
| 680 | // might be multiple vlan in same interface |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 681 | vlanId = getVlanIdFromRelayAgentOption(dhcpPayload); |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 682 | } |
| 683 | if (vlanId == null) { |
| 684 | vlanId = VlanId.NONE; |
| 685 | } |
| 686 | etherReply.setVlanID(vlanId.toShort()); |
| 687 | etherReply.setSourceMACAddress(clientInterface.mac()); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 688 | |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 689 | if (!directlyConnected(dhcpPayload)) { |
| 690 | // if client is indirectly connected, try use next hop mac address |
| 691 | MacAddress macAddress = MacAddress.valueOf(dhcpPayload.getClientHardwareAddress()); |
| 692 | HostId hostId = HostId.hostId(macAddress, vlanId); |
| 693 | DhcpRecord record = dhcpRelayStore.getDhcpRecord(hostId).orElse(null); |
| 694 | if (record != null) { |
| 695 | // if next hop can be found, use mac address of next hop |
| 696 | record.nextHop().ifPresent(etherReply::setDestinationMACAddress); |
| 697 | } else { |
| 698 | // otherwise, discard the packet |
| 699 | log.warn("Can't find record for host id {}, discard packet", hostId); |
| 700 | return null; |
| 701 | } |
Yi Tseng | 1696f56 | 2017-08-17 17:43:38 -0700 | [diff] [blame] | 702 | } else { |
| 703 | etherReply.setDestinationMACAddress(dhcpPayload.getClientHardwareAddress()); |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 704 | } |
| 705 | |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 706 | // we leave the srcMac from the original packet |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 707 | // figure out the relay agent IP corresponding to the original request |
Yi Tseng | 3df7f9d | 2017-08-17 13:08:31 -0700 | [diff] [blame] | 708 | Ip4Address ipFacingClient = getFirstIpFromInterface(clientInterface); |
| 709 | if (ipFacingClient == null) { |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 710 | log.warn("Cannot determine relay agent interface Ipv4 addr for host {}/{}. " |
| 711 | + "Aborting relay for dhcp packet from server {}", |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 712 | etherReply.getDestinationMAC(), clientInterface.vlan(), |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 713 | ethernetPacket); |
| 714 | return null; |
| 715 | } |
| 716 | // SRC_IP: relay agent IP |
| 717 | // DST_IP: offered IP |
Yi Tseng | 3df7f9d | 2017-08-17 13:08:31 -0700 | [diff] [blame] | 718 | ipv4Packet.setSourceAddress(ipFacingClient.toInt()); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 719 | ipv4Packet.setDestinationAddress(dhcpPayload.getYourIPAddress()); |
| 720 | udpPacket.setSourcePort(UDP.DHCP_SERVER_PORT); |
| 721 | if (directlyConnected(dhcpPayload)) { |
| 722 | udpPacket.setDestinationPort(UDP.DHCP_CLIENT_PORT); |
| 723 | } else { |
| 724 | // forward to another dhcp relay |
Yi Tseng | 93ba53c | 2017-09-14 13:24:21 -0700 | [diff] [blame] | 725 | // FIXME: Currently we assume the DHCP comes from a L2 relay with |
| 726 | // Option 82, this might not work if DHCP message comes from |
| 727 | // L3 relay. |
| 728 | udpPacket.setDestinationPort(UDP.DHCP_CLIENT_PORT); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | udpPacket.setPayload(dhcpPayload); |
| 732 | ipv4Packet.setPayload(udpPacket); |
| 733 | etherReply.setPayload(ipv4Packet); |
| 734 | return etherReply; |
| 735 | } |
| 736 | |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 737 | /** |
| 738 | * Extracts VLAN ID from relay agent option. |
| 739 | * |
| 740 | * @param dhcpPayload the DHCP payload |
| 741 | * @return VLAN ID from DHCP payload; null if not exists |
| 742 | */ |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 743 | private VlanId getVlanIdFromRelayAgentOption(DHCP dhcpPayload) { |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 744 | DhcpRelayAgentOption option = (DhcpRelayAgentOption) dhcpPayload.getOption(OptionCode_CircuitID); |
| 745 | if (option == null) { |
| 746 | return null; |
| 747 | } |
| 748 | DhcpOption circuitIdSubOption = option.getSubOption(CIRCUIT_ID.getValue()); |
| 749 | if (circuitIdSubOption == null) { |
| 750 | return null; |
| 751 | } |
| 752 | try { |
| 753 | CircuitId circuitId = CircuitId.deserialize(circuitIdSubOption.getData()); |
| 754 | return circuitId.vlanId(); |
| 755 | } catch (IllegalArgumentException e) { |
| 756 | // can't deserialize the circuit ID |
| 757 | return null; |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | /** |
| 762 | * Removes DHCP relay agent information option (option 82) from DHCP payload. |
| 763 | * Also reset giaddr to 0 |
| 764 | * |
| 765 | * @param ethPacket the Ethernet packet to be processed |
| 766 | * @return Ethernet packet processed |
| 767 | */ |
| 768 | private Ethernet removeRelayAgentOption(Ethernet ethPacket) { |
| 769 | Ethernet ethernet = (Ethernet) ethPacket.clone(); |
| 770 | IPv4 ipv4 = (IPv4) ethernet.getPayload(); |
| 771 | UDP udp = (UDP) ipv4.getPayload(); |
| 772 | DHCP dhcpPayload = (DHCP) udp.getPayload(); |
| 773 | |
| 774 | // removes relay agent information option |
| 775 | List<DhcpOption> options = dhcpPayload.getOptions(); |
| 776 | options = options.stream() |
| 777 | .filter(option -> option.getCode() != OptionCode_CircuitID.getValue()) |
| 778 | .collect(Collectors.toList()); |
| 779 | dhcpPayload.setOptions(options); |
| 780 | dhcpPayload.setGatewayIPAddress(0); |
| 781 | |
| 782 | udp.setPayload(dhcpPayload); |
| 783 | ipv4.setPayload(udp); |
| 784 | ethernet.setPayload(ipv4); |
| 785 | return ethernet; |
| 786 | } |
| 787 | |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 788 | |
| 789 | /** |
| 790 | * Check if the host is directly connected to the network or not. |
| 791 | * |
| 792 | * @param dhcpPayload the dhcp payload |
| 793 | * @return true if the host is directly connected to the network; false otherwise |
| 794 | */ |
| 795 | private boolean directlyConnected(DHCP dhcpPayload) { |
Yi Tseng | 440e2b7 | 2017-08-24 14:47:34 -0700 | [diff] [blame] | 796 | DhcpRelayAgentOption relayAgentOption = |
| 797 | (DhcpRelayAgentOption) dhcpPayload.getOption(OptionCode_CircuitID); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 798 | |
| 799 | // Doesn't contains relay option |
| 800 | if (relayAgentOption == null) { |
| 801 | return true; |
| 802 | } |
| 803 | |
Yi Tseng | 440e2b7 | 2017-08-24 14:47:34 -0700 | [diff] [blame] | 804 | // check circuit id, if circuit id is invalid, we say it is an indirect host |
| 805 | DhcpOption circuitIdOpt = relayAgentOption.getSubOption(CIRCUIT_ID.getValue()); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 806 | |
Yi Tseng | 440e2b7 | 2017-08-24 14:47:34 -0700 | [diff] [blame] | 807 | try { |
| 808 | CircuitId.deserialize(circuitIdOpt.getData()); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 809 | return true; |
Yi Tseng | 440e2b7 | 2017-08-24 14:47:34 -0700 | [diff] [blame] | 810 | } catch (Exception e) { |
| 811 | // invalid circuit id |
| 812 | return false; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 813 | } |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | |
| 817 | /** |
| 818 | * Send the DHCP ack to the requester host. |
| 819 | * Modify Host or Route store according to the type of DHCP. |
| 820 | * |
| 821 | * @param ethernetPacketAck the packet |
| 822 | * @param dhcpPayload the DHCP data |
| 823 | */ |
| 824 | private void handleDhcpAck(Ethernet ethernetPacketAck, DHCP dhcpPayload) { |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 825 | Optional<Interface> outInterface = getClientInterface(ethernetPacketAck, dhcpPayload); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 826 | if (!outInterface.isPresent()) { |
| 827 | log.warn("Can't find output interface for dhcp: {}", dhcpPayload); |
| 828 | return; |
| 829 | } |
| 830 | |
| 831 | Interface outIface = outInterface.get(); |
| 832 | HostLocation hostLocation = new HostLocation(outIface.connectPoint(), System.currentTimeMillis()); |
| 833 | MacAddress macAddress = MacAddress.valueOf(dhcpPayload.getClientHardwareAddress()); |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 834 | VlanId vlanId = getVlanIdFromRelayAgentOption(dhcpPayload); |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 835 | if (vlanId == null) { |
| 836 | vlanId = outIface.vlan(); |
| 837 | } |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 838 | HostId hostId = HostId.hostId(macAddress, vlanId); |
| 839 | Ip4Address ip = Ip4Address.valueOf(dhcpPayload.getYourIPAddress()); |
| 840 | |
| 841 | if (directlyConnected(dhcpPayload)) { |
| 842 | // Add to host store if it connect to network directly |
| 843 | Set<IpAddress> ips = Sets.newHashSet(ip); |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 844 | Host host = hostService.getHost(hostId); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 845 | |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 846 | Set<HostLocation> hostLocations = Sets.newHashSet(hostLocation); |
| 847 | if (host != null) { |
| 848 | // Dual homing support: |
| 849 | // if host exists, use old locations and new location |
| 850 | hostLocations.addAll(host.locations()); |
| 851 | } |
| 852 | HostDescription desc = new DefaultHostDescription(macAddress, vlanId, |
| 853 | hostLocations, ips, false); |
| 854 | // Add IP address when dhcp server give the host new ip address |
| 855 | providerService.hostDetected(hostId, desc, false); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 856 | } else { |
| 857 | // Add to route store if it does not connect to network directly |
| 858 | // Get gateway host IP according to host mac address |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 859 | // TODO: remove relay store here |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 860 | DhcpRecord record = dhcpRelayStore.getDhcpRecord(hostId).orElse(null); |
| 861 | |
| 862 | if (record == null) { |
| 863 | log.warn("Can't find DHCP record of host {}", hostId); |
| 864 | return; |
| 865 | } |
| 866 | |
| 867 | MacAddress gwMac = record.nextHop().orElse(null); |
| 868 | if (gwMac == null) { |
| 869 | log.warn("Can't find gateway mac address from record {}", record); |
| 870 | return; |
| 871 | } |
| 872 | |
| 873 | HostId gwHostId = HostId.hostId(gwMac, record.vlanId()); |
| 874 | Host gwHost = hostService.getHost(gwHostId); |
| 875 | |
| 876 | if (gwHost == null) { |
| 877 | log.warn("Can't find gateway host {}", gwHostId); |
| 878 | return; |
| 879 | } |
| 880 | |
| 881 | Ip4Address nextHopIp = gwHost.ipAddresses() |
| 882 | .stream() |
| 883 | .filter(IpAddress::isIp4) |
| 884 | .map(IpAddress::getIp4Address) |
| 885 | .findFirst() |
| 886 | .orElse(null); |
| 887 | |
| 888 | if (nextHopIp == null) { |
| 889 | log.warn("Can't find IP address of gateway {}", gwHost); |
| 890 | return; |
| 891 | } |
| 892 | |
| 893 | Route route = new Route(Route.Source.STATIC, ip.toIpPrefix(), nextHopIp); |
| 894 | routeStore.updateRoute(route); |
| 895 | } |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | /** |
| 899 | * forward the packet to ConnectPoint where the DHCP server is attached. |
| 900 | * |
| 901 | * @param packet the packet |
| 902 | */ |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 903 | private void handleDhcpDiscoverAndRequest(Ethernet packet, DHCP dhcpPayload) { |
| 904 | ConnectPoint portToFotward = dhcpServerConnectPoint; |
| 905 | if (!directlyConnected(dhcpPayload) && indirectDhcpServerConnectPoint != null) { |
| 906 | portToFotward = indirectDhcpServerConnectPoint; |
| 907 | } |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 908 | // send packet to dhcp server connect point. |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 909 | if (portToFotward != null) { |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 910 | TrafficTreatment t = DefaultTrafficTreatment.builder() |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 911 | .setOutput(portToFotward.port()).build(); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 912 | OutboundPacket o = new DefaultOutboundPacket( |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 913 | portToFotward.deviceId(), t, ByteBuffer.wrap(packet.serialize())); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 914 | if (log.isTraceEnabled()) { |
| 915 | log.trace("Relaying packet to dhcp server {}", packet); |
| 916 | } |
| 917 | packetService.emit(o); |
| 918 | } else { |
| 919 | log.warn("Can't find DHCP server connect point, abort."); |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | |
| 924 | /** |
| 925 | * Gets output interface of a dhcp packet. |
| 926 | * If option 82 exists in the dhcp packet and the option was sent by |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 927 | * ONOS (circuit format is correct), use the connect |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 928 | * point and vlan id from circuit id; otherwise, find host by destination |
| 929 | * address and use vlan id from sender (dhcp server). |
| 930 | * |
| 931 | * @param ethPacket the ethernet packet |
| 932 | * @param dhcpPayload the dhcp packet |
| 933 | * @return an interface represent the output port and vlan; empty value |
| 934 | * if the host or circuit id not found |
| 935 | */ |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 936 | private Optional<Interface> getClientInterface(Ethernet ethPacket, DHCP dhcpPayload) { |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 937 | VlanId originalPacketVlanId = VlanId.vlanId(ethPacket.getVlanID()); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 938 | DhcpRelayAgentOption option = (DhcpRelayAgentOption) dhcpPayload.getOption(OptionCode_CircuitID); |
| 939 | |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 940 | DhcpOption circuitIdSubOption = option.getSubOption(CIRCUIT_ID.getValue()); |
| 941 | try { |
| 942 | CircuitId circuitId = CircuitId.deserialize(circuitIdSubOption.getData()); |
| 943 | ConnectPoint connectPoint = ConnectPoint.deviceConnectPoint(circuitId.connectPoint()); |
| 944 | VlanId vlanId = circuitId.vlanId(); |
| 945 | return interfaceService.getInterfacesByPort(connectPoint) |
| 946 | .stream() |
| 947 | .filter(iface -> interfaceContainsVlan(iface, vlanId)) |
| 948 | .findFirst(); |
| 949 | } catch (IllegalArgumentException ex) { |
| 950 | // invalid circuit format, didn't sent by ONOS |
| 951 | log.debug("Invalid circuit {}, use information from dhcp payload", |
| 952 | circuitIdSubOption.getData()); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | // Use Vlan Id from DHCP server if DHCP relay circuit id was not |
| 956 | // sent by ONOS or circuit Id can't be parsed |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 957 | // TODO: remove relay store from this method |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 958 | MacAddress dstMac = valueOf(dhcpPayload.getClientHardwareAddress()); |
| 959 | Optional<DhcpRecord> dhcpRecord = dhcpRelayStore.getDhcpRecord(HostId.hostId(dstMac, originalPacketVlanId)); |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 960 | ConnectPoint clientConnectPoint = dhcpRecord |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 961 | .map(DhcpRecord::locations) |
| 962 | .orElse(Collections.emptySet()) |
| 963 | .stream() |
| 964 | .reduce((hl1, hl2) -> { |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 965 | // find latest host connect point |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 966 | if (hl1 == null || hl2 == null) { |
| 967 | return hl1 == null ? hl2 : hl1; |
| 968 | } |
| 969 | return hl1.time() > hl2.time() ? hl1 : hl2; |
| 970 | }) |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 971 | .orElse(null); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 972 | |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 973 | if (clientConnectPoint != null) { |
| 974 | return interfaceService.getInterfacesByPort(clientConnectPoint) |
| 975 | .stream() |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 976 | .filter(iface -> interfaceContainsVlan(iface, originalPacketVlanId)) |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 977 | .findFirst(); |
| 978 | } |
| 979 | return Optional.empty(); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | /** |
| 983 | * Send the response DHCP to the requester host. |
| 984 | * |
| 985 | * @param ethPacket the packet |
| 986 | * @param dhcpPayload the DHCP data |
| 987 | */ |
| 988 | private void sendResponseToClient(Ethernet ethPacket, DHCP dhcpPayload) { |
Yi Tseng | dcef2c2 | 2017-08-05 20:34:06 -0700 | [diff] [blame] | 989 | Optional<Interface> outInterface = getClientInterface(ethPacket, dhcpPayload); |
| 990 | if (directlyConnected(dhcpPayload)) { |
| 991 | ethPacket = removeRelayAgentOption(ethPacket); |
| 992 | } |
| 993 | if (!outInterface.isPresent()) { |
| 994 | log.warn("Can't find output interface for client, ignore"); |
| 995 | return; |
| 996 | } |
| 997 | Interface outIface = outInterface.get(); |
| 998 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 999 | .setOutput(outIface.connectPoint().port()) |
| 1000 | .build(); |
| 1001 | OutboundPacket o = new DefaultOutboundPacket( |
| 1002 | outIface.connectPoint().deviceId(), |
| 1003 | treatment, |
| 1004 | ByteBuffer.wrap(ethPacket.serialize())); |
| 1005 | if (log.isTraceEnabled()) { |
| 1006 | log.trace("Relaying packet to DHCP client {} via {}, vlan {}", |
| 1007 | ethPacket, |
| 1008 | outIface.connectPoint(), |
| 1009 | outIface.vlan()); |
| 1010 | } |
| 1011 | packetService.emit(o); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 1012 | } |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 1013 | |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 1014 | @Override |
| 1015 | public void triggerProbe(Host host) { |
| 1016 | // Do nothing here |
| 1017 | } |
| 1018 | |
| 1019 | @Override |
| 1020 | public ProviderId id() { |
Charles Chan | d988c28 | 2017-09-12 17:09:32 -0700 | [diff] [blame] | 1021 | return PROVIDER_ID; |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 1022 | } |
| 1023 | |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 1024 | class InternalHostListener implements HostListener { |
| 1025 | @Override |
| 1026 | public void event(HostEvent event) { |
| 1027 | switch (event.type()) { |
| 1028 | case HOST_ADDED: |
| 1029 | case HOST_UPDATED: |
| 1030 | hostUpdated(event.subject()); |
| 1031 | break; |
| 1032 | case HOST_REMOVED: |
| 1033 | hostRemoved(event.subject()); |
| 1034 | break; |
| 1035 | case HOST_MOVED: |
| 1036 | hostMoved(event.subject()); |
| 1037 | break; |
| 1038 | default: |
| 1039 | break; |
| 1040 | } |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | /** |
| 1045 | * Handle host move. |
| 1046 | * If the host DHCP server or gateway and it moved to the location different |
| 1047 | * to user configured, unsets the connect mac and vlan |
| 1048 | * |
| 1049 | * @param host the host |
| 1050 | */ |
| 1051 | private void hostMoved(Host host) { |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 1052 | if (this.dhcpGatewayIp != null) { |
| 1053 | if (host.ipAddresses().contains(this.dhcpGatewayIp) && |
| 1054 | !host.locations().contains(this.dhcpServerConnectPoint)) { |
| 1055 | this.dhcpConnectMac = null; |
| 1056 | this.dhcpConnectVlan = null; |
| 1057 | } |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 1058 | } |
| 1059 | if (this.dhcpServerIp != null) { |
| 1060 | if (host.ipAddresses().contains(this.dhcpServerIp) && |
| 1061 | !host.locations().contains(this.dhcpServerConnectPoint)) { |
| 1062 | this.dhcpConnectMac = null; |
| 1063 | this.dhcpConnectVlan = null; |
| 1064 | } |
| 1065 | } |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 1066 | if (this.indirectDhcpGatewayIp != null) { |
| 1067 | if (host.ipAddresses().contains(this.indirectDhcpGatewayIp) && |
| 1068 | !host.locations().contains(this.indirectDhcpServerConnectPoint)) { |
| 1069 | this.indirectDhcpConnectMac = null; |
| 1070 | this.indirectDhcpConnectVlan = null; |
| 1071 | } |
| 1072 | } |
| 1073 | if (this.indirectDhcpServerIp != null) { |
| 1074 | if (host.ipAddresses().contains(this.indirectDhcpServerIp) && |
| 1075 | !host.locations().contains(this.indirectDhcpServerConnectPoint)) { |
| 1076 | this.indirectDhcpConnectMac = null; |
| 1077 | this.indirectDhcpConnectVlan = null; |
| 1078 | } |
| 1079 | } |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | /** |
| 1083 | * Handle host updated. |
| 1084 | * If the host is DHCP server or gateway, update connect mac and vlan. |
| 1085 | * |
| 1086 | * @param host the host |
| 1087 | */ |
| 1088 | private void hostUpdated(Host host) { |
| 1089 | if (this.dhcpGatewayIp != null) { |
| 1090 | if (host.ipAddresses().contains(this.dhcpGatewayIp)) { |
| 1091 | this.dhcpConnectMac = host.mac(); |
| 1092 | this.dhcpConnectVlan = host.vlan(); |
| 1093 | } |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 1094 | } |
| 1095 | if (this.dhcpServerIp != null) { |
| 1096 | if (host.ipAddresses().contains(this.dhcpServerIp)) { |
| 1097 | this.dhcpConnectMac = host.mac(); |
| 1098 | this.dhcpConnectVlan = host.vlan(); |
| 1099 | } |
| 1100 | } |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 1101 | if (this.indirectDhcpGatewayIp != null) { |
| 1102 | if (host.ipAddresses().contains(this.indirectDhcpGatewayIp)) { |
| 1103 | this.indirectDhcpConnectMac = host.mac(); |
| 1104 | this.indirectDhcpConnectVlan = host.vlan(); |
| 1105 | } |
| 1106 | } |
| 1107 | if (this.indirectDhcpServerIp != null) { |
| 1108 | if (host.ipAddresses().contains(this.indirectDhcpServerIp)) { |
| 1109 | this.indirectDhcpConnectMac = host.mac(); |
| 1110 | this.indirectDhcpConnectVlan = host.vlan(); |
| 1111 | } |
| 1112 | } |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | /** |
| 1116 | * Handle host removed. |
| 1117 | * If the host is DHCP server or gateway, unset connect mac and vlan. |
| 1118 | * |
| 1119 | * @param host the host |
| 1120 | */ |
| 1121 | private void hostRemoved(Host host) { |
| 1122 | if (this.dhcpGatewayIp != null) { |
| 1123 | if (host.ipAddresses().contains(this.dhcpGatewayIp)) { |
| 1124 | this.dhcpConnectMac = null; |
| 1125 | this.dhcpConnectVlan = null; |
| 1126 | } |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 1127 | } |
| 1128 | if (this.dhcpServerIp != null) { |
| 1129 | if (host.ipAddresses().contains(this.dhcpServerIp)) { |
| 1130 | this.dhcpConnectMac = null; |
| 1131 | this.dhcpConnectVlan = null; |
| 1132 | } |
| 1133 | } |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 1134 | if (this.indirectDhcpGatewayIp != null) { |
| 1135 | if (host.ipAddresses().contains(this.indirectDhcpGatewayIp)) { |
| 1136 | this.indirectDhcpConnectMac = null; |
| 1137 | this.indirectDhcpConnectVlan = null; |
| 1138 | } |
| 1139 | } |
| 1140 | if (this.indirectDhcpServerIp != null) { |
| 1141 | if (host.ipAddresses().contains(this.indirectDhcpServerIp)) { |
| 1142 | this.indirectDhcpConnectMac = null; |
| 1143 | this.indirectDhcpConnectVlan = null; |
| 1144 | } |
| 1145 | } |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 1146 | } |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 1147 | } |