Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -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.dhcprelay; |
| 17 | |
| 18 | import java.nio.ByteBuffer; |
| 19 | import java.util.Collection; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 20 | import java.util.Dictionary; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 21 | import java.util.Objects; |
| 22 | import java.util.Optional; |
| 23 | import java.util.Set; |
| 24 | import java.util.stream.Stream; |
| 25 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Activate; |
| 27 | import org.apache.felix.scr.annotations.Component; |
| 28 | import org.apache.felix.scr.annotations.Deactivate; |
| 29 | import org.apache.felix.scr.annotations.Modified; |
| 30 | import org.apache.felix.scr.annotations.Property; |
| 31 | import org.apache.felix.scr.annotations.Reference; |
| 32 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 33 | import org.apache.felix.scr.annotations.Service; |
| 34 | import org.onlab.packet.ARP; |
| 35 | import org.onlab.packet.DHCP; |
| 36 | import org.onlab.packet.DHCP6; |
| 37 | import org.onlab.packet.IPacket; |
| 38 | import org.onlab.packet.IPv6; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 39 | import org.onlab.packet.Ethernet; |
| 40 | import org.onlab.packet.IPv4; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 41 | import org.onlab.packet.IpAddress; |
| 42 | import org.onlab.packet.MacAddress; |
| 43 | import org.onlab.packet.TpPort; |
| 44 | import org.onlab.packet.UDP; |
| 45 | import org.onlab.packet.VlanId; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 46 | import org.onlab.util.Tools; |
| 47 | import org.onosproject.cfg.ComponentConfigService; |
| 48 | import org.onosproject.core.ApplicationId; |
| 49 | import org.onosproject.core.CoreService; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 50 | import org.onosproject.dhcprelay.api.DhcpHandler; |
| 51 | import org.onosproject.dhcprelay.api.DhcpRelayService; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 52 | import org.onosproject.dhcprelay.store.DhcpRecord; |
| 53 | import org.onosproject.dhcprelay.store.DhcpRelayStore; |
Ray Milkey | facf286 | 2017-08-03 11:58:29 -0700 | [diff] [blame] | 54 | import org.onosproject.net.intf.Interface; |
| 55 | import org.onosproject.net.intf.InterfaceService; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 56 | import org.onosproject.net.ConnectPoint; |
| 57 | import org.onosproject.net.Host; |
| 58 | import org.onosproject.net.HostId; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 59 | import org.onosproject.net.config.ConfigFactory; |
| 60 | import org.onosproject.net.config.NetworkConfigEvent; |
| 61 | import org.onosproject.net.config.NetworkConfigListener; |
| 62 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 63 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 64 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 65 | import org.onosproject.net.flow.TrafficSelector; |
| 66 | import org.onosproject.net.flow.TrafficTreatment; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 67 | import org.onosproject.net.host.HostEvent; |
| 68 | import org.onosproject.net.host.HostListener; |
| 69 | import org.onosproject.net.host.HostService; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 70 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 71 | import org.onosproject.net.packet.OutboundPacket; |
| 72 | import org.onosproject.net.packet.PacketContext; |
| 73 | import org.onosproject.net.packet.PacketPriority; |
| 74 | import org.onosproject.net.packet.PacketProcessor; |
| 75 | import org.onosproject.net.packet.PacketService; |
| 76 | import org.onosproject.net.provider.ProviderId; |
| 77 | import org.osgi.service.component.ComponentContext; |
| 78 | import org.slf4j.Logger; |
| 79 | import org.slf4j.LoggerFactory; |
| 80 | |
| 81 | import com.google.common.collect.ImmutableSet; |
| 82 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 83 | import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 84 | /** |
| 85 | * DHCP Relay Agent Application Component. |
| 86 | */ |
| 87 | @Component(immediate = true) |
| 88 | @Service |
| 89 | public class DhcpRelayManager implements DhcpRelayService { |
| 90 | public static final String DHCP_RELAY_APP = "org.onosproject.dhcp-relay"; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 91 | public static final ProviderId PROVIDER_ID = new ProviderId("host", DHCP_RELAY_APP); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 92 | public static final String HOST_LOCATION_PROVIDER = |
| 93 | "org.onosproject.provider.host.impl.HostLocationProvider"; |
| 94 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 95 | private final InternalConfigListener cfgListener = new InternalConfigListener(); |
| 96 | |
| 97 | private final Set<ConfigFactory> factories = ImmutableSet.of( |
| 98 | new ConfigFactory<ApplicationId, DhcpRelayConfig>(APP_SUBJECT_FACTORY, |
| 99 | DhcpRelayConfig.class, |
| 100 | "dhcprelay") { |
| 101 | @Override |
| 102 | public DhcpRelayConfig createConfig() { |
| 103 | return new DhcpRelayConfig(); |
| 104 | } |
| 105 | } |
| 106 | ); |
| 107 | |
| 108 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 109 | protected NetworkConfigRegistry cfgService; |
| 110 | |
| 111 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 112 | protected CoreService coreService; |
| 113 | |
| 114 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 115 | protected PacketService packetService; |
| 116 | |
| 117 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 118 | protected HostService hostService; |
| 119 | |
| 120 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 121 | protected InterfaceService interfaceService; |
| 122 | |
| 123 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 124 | protected DhcpRelayStore dhcpRelayStore; |
| 125 | |
| 126 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 127 | protected ComponentConfigService compCfgService; |
| 128 | |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 129 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY, |
| 130 | target = "(version=4)") |
| 131 | protected DhcpHandler v4Handler; |
| 132 | |
| 133 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY, |
| 134 | target = "(version=6)") |
| 135 | protected DhcpHandler v6Handler; |
| 136 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 137 | @Property(name = "arpEnabled", boolValue = true, |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 138 | label = "Enable Address resolution protocol") |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 139 | protected boolean arpEnabled = true; |
| 140 | |
| 141 | private DhcpRelayPacketProcessor dhcpRelayPacketProcessor = new DhcpRelayPacketProcessor(); |
| 142 | private InternalHostListener hostListener = new InternalHostListener(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 143 | private ApplicationId appId; |
| 144 | |
| 145 | @Activate |
| 146 | protected void activate(ComponentContext context) { |
| 147 | //start the dhcp relay agent |
| 148 | appId = coreService.registerApplication(DHCP_RELAY_APP); |
| 149 | |
| 150 | cfgService.addListener(cfgListener); |
| 151 | factories.forEach(cfgService::registerConfigFactory); |
| 152 | //update the dhcp server configuration. |
| 153 | updateConfig(); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 154 | |
| 155 | //add the packet processor |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 156 | packetService.addProcessor(dhcpRelayPacketProcessor, PacketProcessor.director(0)); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 157 | |
| 158 | // listen host event for dhcp server or the gateway |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 159 | hostService.addListener(hostListener); |
| 160 | requestDhcpPackets(); |
| 161 | modified(context); |
| 162 | |
| 163 | // disable dhcp from host location provider |
| 164 | compCfgService.preSetProperty(HOST_LOCATION_PROVIDER, |
| 165 | "useDhcp", Boolean.FALSE.toString()); |
| 166 | compCfgService.registerProperties(getClass()); |
| 167 | log.info("DHCP-RELAY Started"); |
| 168 | } |
| 169 | |
| 170 | @Deactivate |
| 171 | protected void deactivate() { |
| 172 | cfgService.removeListener(cfgListener); |
| 173 | factories.forEach(cfgService::unregisterConfigFactory); |
| 174 | packetService.removeProcessor(dhcpRelayPacketProcessor); |
| 175 | hostService.removeListener(hostListener); |
| 176 | cancelDhcpPackets(); |
| 177 | cancelArpPackets(); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 178 | v4Handler.getDhcpGatewayIp().ifPresent(hostService::stopMonitoringIp); |
| 179 | v4Handler.getDhcpServerIp().ifPresent(hostService::stopMonitoringIp); |
| 180 | // TODO: DHCPv6 Handler |
| 181 | |
Yi Tseng | dfa8ee2 | 2017-08-07 12:45:01 -0700 | [diff] [blame] | 182 | compCfgService.unregisterProperties(getClass(), false); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 183 | log.info("DHCP-RELAY Stopped"); |
| 184 | } |
| 185 | |
| 186 | @Modified |
| 187 | protected void modified(ComponentContext context) { |
| 188 | Dictionary<?, ?> properties = context.getProperties(); |
| 189 | Boolean flag; |
| 190 | |
| 191 | flag = Tools.isPropertyEnabled(properties, "arpEnabled"); |
| 192 | if (flag != null) { |
| 193 | arpEnabled = flag; |
| 194 | log.info("Address resolution protocol is {}", |
| 195 | arpEnabled ? "enabled" : "disabled"); |
| 196 | } |
| 197 | |
| 198 | if (arpEnabled) { |
| 199 | requestArpPackets(); |
| 200 | } else { |
| 201 | cancelArpPackets(); |
| 202 | } |
| 203 | } |
| 204 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 205 | private void updateConfig() { |
| 206 | DhcpRelayConfig cfg = cfgService.getConfig(appId, DhcpRelayConfig.class); |
| 207 | if (cfg == null) { |
| 208 | log.warn("Dhcp Server info not available"); |
| 209 | return; |
| 210 | } |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 211 | Optional<IpAddress> oldDhcpServerIp = v4Handler.getDhcpServerIp(); |
| 212 | Optional<IpAddress> oldDhcpGatewayIp = v4Handler.getDhcpGatewayIp(); |
| 213 | v4Handler.setDhcpServerConnectPoint(cfg.getDhcpServerConnectPoint()); |
| 214 | v4Handler.setDhcpServerIp(cfg.getDhcpServerIp()); |
| 215 | v4Handler.setDhcpGatewayIp(cfg.getDhcpGatewayIp()); |
| 216 | v4Handler.setDhcpConnectMac(null); |
| 217 | v4Handler.setDhcpConnectVlan(null); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 218 | |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 219 | log.info("DHCP server connect point: " + cfg.getDhcpServerConnectPoint()); |
| 220 | log.info("DHCP server ipaddress " + cfg.getDhcpServerIp()); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 221 | |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 222 | IpAddress ipToProbe = v4Handler.getDhcpGatewayIp().isPresent() ? cfg.getDhcpGatewayIp() : |
| 223 | cfg.getDhcpServerIp(); |
| 224 | String hostToProbe = v4Handler.getDhcpGatewayIp().isPresent() ? "gateway" : "DHCP server"; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 225 | |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 226 | // TODO: DHCPv6 server config |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 227 | Set<Host> hosts = hostService.getHostsByIp(ipToProbe); |
| 228 | if (hosts.isEmpty()) { |
| 229 | log.info("Probing to resolve {} IP {}", hostToProbe, ipToProbe); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 230 | oldDhcpGatewayIp.ifPresent(hostService::stopMonitoringIp); |
| 231 | oldDhcpServerIp.ifPresent(hostService::stopMonitoringIp); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 232 | hostService.startMonitoringIp(ipToProbe); |
| 233 | } else { |
| 234 | // Probe target is known; There should be only 1 host with this ip |
| 235 | hostUpdated(hosts.iterator().next()); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | private void hostRemoved(Host host) { |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 240 | v4Handler.getDhcpServerIp().ifPresent(ip -> { |
| 241 | if (host.ipAddresses().contains(ip)) { |
| 242 | log.warn("DHCP server {} removed", ip); |
| 243 | v4Handler.setDhcpConnectMac(null); |
| 244 | v4Handler.setDhcpConnectVlan(null); |
| 245 | } |
| 246 | }); |
| 247 | v4Handler.getDhcpGatewayIp().ifPresent(ip -> { |
| 248 | if (host.ipAddresses().contains(ip)) { |
| 249 | log.warn("DHCP gateway {} removed", ip); |
| 250 | v4Handler.setDhcpConnectMac(null); |
| 251 | v4Handler.setDhcpConnectVlan(null); |
| 252 | } |
| 253 | }); |
| 254 | // TODO: v6 handler |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | private void hostUpdated(Host host) { |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 258 | v4Handler.getDhcpGatewayIp().ifPresent(ip -> { |
| 259 | if (host.ipAddresses().contains(ip)) { |
| 260 | log.warn("DHCP gateway {} removed", ip); |
| 261 | v4Handler.setDhcpConnectMac(host.mac()); |
| 262 | v4Handler.setDhcpConnectVlan(host.vlan()); |
| 263 | } |
| 264 | }); |
| 265 | v4Handler.getDhcpServerIp().ifPresent(ip -> { |
| 266 | if (host.ipAddresses().contains(ip)) { |
| 267 | log.warn("DHCP server {} removed", ip); |
| 268 | v4Handler.setDhcpConnectMac(host.mac()); |
| 269 | v4Handler.setDhcpConnectVlan(host.vlan()); |
| 270 | } |
| 271 | }); |
| 272 | // TODO: v6 handler |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Request DHCP packet in via PacketService. |
| 277 | */ |
| 278 | private void requestDhcpPackets() { |
| 279 | TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder() |
| 280 | .matchEthType(Ethernet.TYPE_IPV4) |
| 281 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
| 282 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_SERVER_PORT)); |
| 283 | packetService.requestPackets(selectorServer.build(), PacketPriority.CONTROL, appId); |
| 284 | |
| 285 | TrafficSelector.Builder selectorClient = DefaultTrafficSelector.builder() |
| 286 | .matchEthType(Ethernet.TYPE_IPV4) |
| 287 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
| 288 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_CLIENT_PORT)); |
| 289 | packetService.requestPackets(selectorClient.build(), PacketPriority.CONTROL, appId); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Cancel requested DHCP packets in via packet service. |
| 294 | */ |
| 295 | private void cancelDhcpPackets() { |
| 296 | TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder() |
| 297 | .matchEthType(Ethernet.TYPE_IPV4) |
| 298 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
| 299 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_SERVER_PORT)); |
| 300 | packetService.cancelPackets(selectorServer.build(), PacketPriority.CONTROL, appId); |
| 301 | |
| 302 | TrafficSelector.Builder selectorClient = DefaultTrafficSelector.builder() |
| 303 | .matchEthType(Ethernet.TYPE_IPV4) |
| 304 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
| 305 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_CLIENT_PORT)); |
| 306 | packetService.cancelPackets(selectorClient.build(), PacketPriority.CONTROL, appId); |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Request ARP packet in via PacketService. |
| 311 | */ |
| 312 | private void requestArpPackets() { |
| 313 | TrafficSelector.Builder selectorArpServer = DefaultTrafficSelector.builder() |
| 314 | .matchEthType(Ethernet.TYPE_ARP); |
| 315 | packetService.requestPackets(selectorArpServer.build(), PacketPriority.CONTROL, appId); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Cancel requested ARP packets in via packet service. |
| 320 | */ |
| 321 | private void cancelArpPackets() { |
| 322 | TrafficSelector.Builder selectorArpServer = DefaultTrafficSelector.builder() |
| 323 | .matchEthType(Ethernet.TYPE_ARP); |
| 324 | packetService.cancelPackets(selectorArpServer.build(), PacketPriority.CONTROL, appId); |
| 325 | } |
| 326 | |
| 327 | @Override |
| 328 | public Optional<DhcpRecord> getDhcpRecord(HostId hostId) { |
| 329 | return dhcpRelayStore.getDhcpRecord(hostId); |
| 330 | } |
| 331 | |
| 332 | @Override |
| 333 | public Collection<DhcpRecord> getDhcpRecords() { |
| 334 | return dhcpRelayStore.getDhcpRecords(); |
| 335 | } |
| 336 | |
Yi Tseng | 13a41a1 | 2017-07-26 13:45:01 -0700 | [diff] [blame] | 337 | @Override |
| 338 | public Optional<MacAddress> getDhcpServerMacAddress() { |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 339 | return v4Handler.getDhcpConnectMac(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Gets DHCP data from a packet. |
| 344 | * |
| 345 | * @param packet the packet |
| 346 | * @return the DHCP data; empty if it is not a DHCP packet |
| 347 | */ |
| 348 | private Optional<DHCP> findDhcp(Ethernet packet) { |
| 349 | return Stream.of(packet) |
| 350 | .filter(Objects::nonNull) |
| 351 | .map(Ethernet::getPayload) |
| 352 | .filter(p -> p instanceof IPv4) |
| 353 | .map(IPacket::getPayload) |
| 354 | .filter(Objects::nonNull) |
| 355 | .filter(p -> p instanceof UDP) |
| 356 | .map(IPacket::getPayload) |
| 357 | .filter(Objects::nonNull) |
| 358 | .filter(p -> p instanceof DHCP) |
| 359 | .map(p -> (DHCP) p) |
| 360 | .findFirst(); |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Gets DHCPv6 data from a packet. |
| 365 | * |
| 366 | * @param packet the packet |
| 367 | * @return the DHCPv6 data; empty if it is not a DHCPv6 packet |
| 368 | */ |
| 369 | private Optional<DHCP6> findDhcp6(Ethernet packet) { |
| 370 | return Stream.of(packet) |
| 371 | .filter(Objects::nonNull) |
| 372 | .map(Ethernet::getPayload) |
| 373 | .filter(p -> p instanceof IPv6) |
| 374 | .map(IPacket::getPayload) |
| 375 | .filter(Objects::nonNull) |
| 376 | .filter(p -> p instanceof UDP) |
| 377 | .map(IPacket::getPayload) |
| 378 | .filter(Objects::nonNull) |
| 379 | .filter(p -> p instanceof DHCP6) |
| 380 | .map(p -> (DHCP6) p) |
| 381 | .findFirst(); |
| 382 | } |
| 383 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 384 | |
| 385 | private class DhcpRelayPacketProcessor implements PacketProcessor { |
| 386 | |
| 387 | @Override |
| 388 | public void process(PacketContext context) { |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 389 | // process the packet and get the payload |
| 390 | Ethernet packet = context.inPacket().parsed(); |
| 391 | if (packet == null) { |
| 392 | return; |
| 393 | } |
| 394 | |
| 395 | findDhcp(packet).ifPresent(dhcpPayload -> { |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 396 | v4Handler.processDhcpPacket(context, dhcpPayload); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 397 | }); |
| 398 | |
| 399 | findDhcp6(packet).ifPresent(dhcp6Payload -> { |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 400 | v6Handler.processDhcpPacket(context, dhcp6Payload); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 401 | }); |
| 402 | |
| 403 | if (packet.getEtherType() == Ethernet.TYPE_ARP && arpEnabled) { |
| 404 | ARP arpPacket = (ARP) packet.getPayload(); |
| 405 | VlanId vlanId = VlanId.vlanId(packet.getVlanID()); |
| 406 | Set<Interface> interfaces = interfaceService. |
| 407 | getInterfacesByPort(context.inPacket().receivedFrom()); |
| 408 | //ignore the packets if dhcp server interface is not configured on onos. |
| 409 | if (interfaces.isEmpty()) { |
| 410 | log.warn("server virtual interface not configured"); |
| 411 | return; |
| 412 | } |
| 413 | if ((arpPacket.getOpCode() != ARP.OP_REQUEST)) { |
| 414 | // handle request only |
| 415 | return; |
| 416 | } |
| 417 | MacAddress interfaceMac = interfaces.stream() |
| 418 | .filter(iface -> iface.vlan().equals(vlanId)) |
| 419 | .map(Interface::mac) |
| 420 | .filter(mac -> !mac.equals(MacAddress.NONE)) |
| 421 | .findFirst() |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 422 | .orElse(MacAddress.ONOS); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 423 | if (interfaceMac == null) { |
| 424 | // can't find interface mac address |
| 425 | return; |
| 426 | } |
| 427 | processArpPacket(context, packet, interfaceMac); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Processes the ARP Payload and initiates a reply to the client. |
| 433 | * |
| 434 | * @param context the packet context |
| 435 | * @param packet the ethernet payload |
| 436 | * @param replyMac mac address to be replied |
| 437 | */ |
| 438 | private void processArpPacket(PacketContext context, Ethernet packet, MacAddress replyMac) { |
| 439 | ARP arpPacket = (ARP) packet.getPayload(); |
| 440 | ARP arpReply = (ARP) arpPacket.clone(); |
| 441 | arpReply.setOpCode(ARP.OP_REPLY); |
| 442 | |
| 443 | arpReply.setTargetProtocolAddress(arpPacket.getSenderProtocolAddress()); |
| 444 | arpReply.setTargetHardwareAddress(arpPacket.getSenderHardwareAddress()); |
| 445 | arpReply.setSenderProtocolAddress(arpPacket.getTargetProtocolAddress()); |
| 446 | arpReply.setSenderHardwareAddress(replyMac.toBytes()); |
| 447 | |
| 448 | // Ethernet Frame. |
| 449 | Ethernet ethReply = new Ethernet(); |
| 450 | ethReply.setSourceMACAddress(replyMac.toBytes()); |
| 451 | ethReply.setDestinationMACAddress(packet.getSourceMAC()); |
| 452 | ethReply.setEtherType(Ethernet.TYPE_ARP); |
| 453 | ethReply.setVlanID(packet.getVlanID()); |
| 454 | ethReply.setPayload(arpReply); |
| 455 | |
| 456 | ConnectPoint targetPort = context.inPacket().receivedFrom(); |
| 457 | TrafficTreatment t = DefaultTrafficTreatment.builder() |
| 458 | .setOutput(targetPort.port()).build(); |
| 459 | OutboundPacket o = new DefaultOutboundPacket( |
| 460 | targetPort.deviceId(), t, ByteBuffer.wrap(ethReply.serialize())); |
| 461 | if (log.isTraceEnabled()) { |
| 462 | log.trace("Relaying ARP packet {} to {}", packet, targetPort); |
| 463 | } |
| 464 | packetService.emit(o); |
| 465 | } |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Listener for network config events. |
| 470 | */ |
| 471 | private class InternalConfigListener implements NetworkConfigListener { |
| 472 | @Override |
| 473 | public void event(NetworkConfigEvent event) { |
| 474 | if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 475 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) && |
| 476 | event.configClass().equals(DhcpRelayConfig.class)) { |
| 477 | updateConfig(); |
| 478 | log.info("Reconfigured"); |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * Internal listener for host events. |
| 485 | */ |
| 486 | private class InternalHostListener implements HostListener { |
| 487 | @Override |
| 488 | public void event(HostEvent event) { |
| 489 | switch (event.type()) { |
| 490 | case HOST_ADDED: |
| 491 | case HOST_UPDATED: |
| 492 | hostUpdated(event.subject()); |
| 493 | break; |
| 494 | case HOST_REMOVED: |
| 495 | hostRemoved(event.subject()); |
| 496 | break; |
| 497 | case HOST_MOVED: |
| 498 | // XXX todo -- moving dhcp server |
| 499 | break; |
| 500 | default: |
| 501 | break; |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | } |