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