gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | |
gaurav | 4af95fb | 2016-07-07 01:48:44 +0530 | [diff] [blame] | 18 | import java.nio.ByteBuffer; |
Pier Luigi | 20557c8 | 2017-01-12 22:40:46 -0800 | [diff] [blame] | 19 | import java.util.Dictionary; |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 20 | import java.util.Objects; |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 21 | import java.util.Set; |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
Pier Luigi | 20557c8 | 2017-01-12 22:40:46 -0800 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Modified; |
| 26 | import org.apache.felix.scr.annotations.Property; |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Reference; |
| 28 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 29 | import org.onlab.packet.ARP; |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 30 | import org.onlab.packet.DHCP; |
gaurav | 4af95fb | 2016-07-07 01:48:44 +0530 | [diff] [blame] | 31 | import org.onlab.packet.DHCPOption; |
| 32 | import org.onlab.packet.DHCPPacketType; |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 33 | import org.onlab.packet.Ethernet; |
| 34 | import org.onlab.packet.IPv4; |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 35 | import org.onlab.packet.Ip4Address; |
| 36 | import org.onlab.packet.MacAddress; |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 37 | import org.onlab.packet.TpPort; |
| 38 | import org.onlab.packet.UDP; |
gaurav | 4af95fb | 2016-07-07 01:48:44 +0530 | [diff] [blame] | 39 | import org.onlab.packet.VlanId; |
Pier Luigi | 20557c8 | 2017-01-12 22:40:46 -0800 | [diff] [blame] | 40 | import org.onlab.util.Tools; |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 41 | import org.onosproject.core.ApplicationId; |
| 42 | import org.onosproject.core.CoreService; |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 43 | import org.onosproject.incubator.net.intf.Interface; |
| 44 | import org.onosproject.incubator.net.intf.InterfaceService; |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 45 | import org.onosproject.net.ConnectPoint; |
| 46 | import org.onosproject.net.Host; |
| 47 | import org.onosproject.net.HostId; |
| 48 | import org.onosproject.net.config.ConfigFactory; |
| 49 | import org.onosproject.net.config.NetworkConfigEvent; |
| 50 | import org.onosproject.net.config.NetworkConfigListener; |
| 51 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 52 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 53 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 54 | import org.onosproject.net.flow.TrafficSelector; |
| 55 | import org.onosproject.net.flow.TrafficTreatment; |
| 56 | import org.onosproject.net.host.HostService; |
| 57 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 58 | import org.onosproject.net.packet.OutboundPacket; |
| 59 | import org.onosproject.net.packet.PacketContext; |
| 60 | import org.onosproject.net.packet.PacketPriority; |
| 61 | import org.onosproject.net.packet.PacketProcessor; |
| 62 | import org.onosproject.net.packet.PacketService; |
Pier Luigi | 20557c8 | 2017-01-12 22:40:46 -0800 | [diff] [blame] | 63 | import org.osgi.service.component.ComponentContext; |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 64 | import org.slf4j.Logger; |
| 65 | import org.slf4j.LoggerFactory; |
| 66 | |
| 67 | import com.google.common.collect.ImmutableSet; |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 68 | |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 69 | import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
gaurav | 4af95fb | 2016-07-07 01:48:44 +0530 | [diff] [blame] | 70 | import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_MessageType; |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 71 | import static org.onlab.packet.MacAddress.valueOf; |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 72 | /** |
| 73 | * DHCP Relay Agent Application Component. |
| 74 | */ |
| 75 | @Component(immediate = true) |
| 76 | public class DhcpRelay { |
| 77 | |
| 78 | public static final String DHCP_RELAY_APP = "org.onosproject.dhcp-relay"; |
| 79 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 80 | private final InternalConfigListener cfgListener = new InternalConfigListener(); |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 81 | private static Ip4Address relayAgentIP = null; |
| 82 | private static MacAddress relayAgentMAC = null; |
| 83 | private static MacAddress myMAC = valueOf("4f:4f:4f:4f:4f:4f"); |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 84 | |
| 85 | private final Set<ConfigFactory> factories = ImmutableSet.of( |
| 86 | new ConfigFactory<ApplicationId, DhcpRelayConfig>(APP_SUBJECT_FACTORY, |
| 87 | DhcpRelayConfig.class, |
| 88 | "dhcprelay") { |
| 89 | @Override |
| 90 | public DhcpRelayConfig createConfig() { |
| 91 | return new DhcpRelayConfig(); |
| 92 | } |
| 93 | } |
| 94 | ); |
| 95 | |
| 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 97 | protected NetworkConfigRegistry cfgService; |
| 98 | |
| 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 100 | protected CoreService coreService; |
| 101 | |
| 102 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 103 | protected PacketService packetService; |
| 104 | |
| 105 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 106 | protected HostService hostService; |
| 107 | |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 108 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 109 | protected InterfaceService interfaceService; |
| 110 | |
Pier Luigi | 20557c8 | 2017-01-12 22:40:46 -0800 | [diff] [blame] | 111 | @Property(name = "arpEnabled", boolValue = true, |
| 112 | label = "Enable Address resolution protocol") |
| 113 | protected boolean arpEnabled = true; |
| 114 | |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 115 | private DhcpRelayPacketProcessor dhcpRelayPacketProcessor = new DhcpRelayPacketProcessor(); |
| 116 | private ConnectPoint dhcpServerConnectPoint = null; |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 117 | private Ip4Address dhcpServerIp = null; |
| 118 | private MacAddress dhcpServerMac = null; |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 119 | private ApplicationId appId; |
| 120 | |
| 121 | @Activate |
Pier Luigi | 20557c8 | 2017-01-12 22:40:46 -0800 | [diff] [blame] | 122 | protected void activate(ComponentContext context) { |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 123 | //start the dhcp relay agent |
| 124 | |
| 125 | appId = coreService.registerApplication(DHCP_RELAY_APP); |
| 126 | |
| 127 | cfgService.addListener(cfgListener); |
| 128 | factories.forEach(cfgService::registerConfigFactory); |
| 129 | //update the dhcp server configuration. |
| 130 | updateConfig(); |
| 131 | //add the packet services. |
| 132 | packetService.addProcessor(dhcpRelayPacketProcessor, PacketProcessor.director(0)); |
Pier Luigi | 20557c8 | 2017-01-12 22:40:46 -0800 | [diff] [blame] | 133 | |
| 134 | requestDhcpPackets(); |
| 135 | modified(context); |
| 136 | |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 137 | log.info("DHCP-RELAY Started"); |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Deactivate |
| 141 | protected void deactivate() { |
| 142 | cfgService.removeListener(cfgListener); |
| 143 | factories.forEach(cfgService::unregisterConfigFactory); |
| 144 | packetService.removeProcessor(dhcpRelayPacketProcessor); |
Pier Luigi | 20557c8 | 2017-01-12 22:40:46 -0800 | [diff] [blame] | 145 | |
| 146 | cancelDhcpPackets(); |
| 147 | cancelArpPackets(); |
| 148 | |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 149 | log.info("DHCP-RELAY Stopped"); |
| 150 | } |
| 151 | |
Pier Luigi | 20557c8 | 2017-01-12 22:40:46 -0800 | [diff] [blame] | 152 | @Modified |
| 153 | protected void modified(ComponentContext context) { |
| 154 | Dictionary<?, ?> properties = context.getProperties(); |
| 155 | Boolean flag; |
| 156 | |
| 157 | flag = Tools.isPropertyEnabled(properties, "arpEnabled"); |
| 158 | if (flag != null) { |
| 159 | arpEnabled = flag; |
| 160 | log.info("Address resolution protocol is {}", |
| 161 | arpEnabled ? "enabled" : "disabled"); |
| 162 | } |
| 163 | |
| 164 | if (arpEnabled) { |
| 165 | requestArpPackets(); |
| 166 | } else { |
| 167 | cancelArpPackets(); |
| 168 | } |
| 169 | } |
| 170 | |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 171 | private void updateConfig() { |
| 172 | DhcpRelayConfig cfg = cfgService.getConfig(appId, DhcpRelayConfig.class); |
| 173 | |
| 174 | if (cfg == null) { |
| 175 | log.warn("Dhcp Server info not available"); |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | dhcpServerConnectPoint = cfg.getDhcpServerConnectPoint(); |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 180 | dhcpServerIp = cfg.getDhcpServerIp(); |
| 181 | dhcpServerMac = cfg.getDhcpServermac(); |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 182 | log.info("dhcp server connect points are " + dhcpServerConnectPoint); |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 183 | log.info("dhcp server ipaddress " + dhcpServerIp); |
| 184 | log.info("dhcp server mac address " + dhcpServerMac); |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /** |
Pier Luigi | 20557c8 | 2017-01-12 22:40:46 -0800 | [diff] [blame] | 188 | * Request DHCP packet in via PacketService. |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 189 | */ |
Pier Luigi | 20557c8 | 2017-01-12 22:40:46 -0800 | [diff] [blame] | 190 | private void requestDhcpPackets() { |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 191 | TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder() |
| 192 | .matchEthType(Ethernet.TYPE_IPV4) |
| 193 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 194 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_SERVER_PORT)); |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 195 | packetService.requestPackets(selectorServer.build(), PacketPriority.CONTROL, appId); |
| 196 | |
| 197 | TrafficSelector.Builder selectorClient = DefaultTrafficSelector.builder() |
| 198 | .matchEthType(Ethernet.TYPE_IPV4) |
| 199 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 200 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_CLIENT_PORT)); |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 201 | packetService.requestPackets(selectorClient.build(), PacketPriority.CONTROL, appId); |
| 202 | } |
| 203 | |
| 204 | /** |
Pier Luigi | 20557c8 | 2017-01-12 22:40:46 -0800 | [diff] [blame] | 205 | * Cancel requested DHCP packets in via packet service. |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 206 | */ |
Pier Luigi | 20557c8 | 2017-01-12 22:40:46 -0800 | [diff] [blame] | 207 | private void cancelDhcpPackets() { |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 208 | TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder() |
| 209 | .matchEthType(Ethernet.TYPE_IPV4) |
| 210 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 211 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_SERVER_PORT)); |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 212 | packetService.cancelPackets(selectorServer.build(), PacketPriority.CONTROL, appId); |
| 213 | |
| 214 | TrafficSelector.Builder selectorClient = DefaultTrafficSelector.builder() |
| 215 | .matchEthType(Ethernet.TYPE_IPV4) |
| 216 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 217 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_CLIENT_PORT)); |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 218 | packetService.cancelPackets(selectorClient.build(), PacketPriority.CONTROL, appId); |
Pier Luigi | 20557c8 | 2017-01-12 22:40:46 -0800 | [diff] [blame] | 219 | } |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 220 | |
Pier Luigi | 20557c8 | 2017-01-12 22:40:46 -0800 | [diff] [blame] | 221 | /** |
| 222 | * Request ARP packet in via PacketService. |
| 223 | */ |
| 224 | private void requestArpPackets() { |
| 225 | TrafficSelector.Builder selectorArpServer = DefaultTrafficSelector.builder() |
| 226 | .matchEthType(Ethernet.TYPE_ARP); |
| 227 | packetService.requestPackets(selectorArpServer.build(), PacketPriority.CONTROL, appId); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Cancel requested ARP packets in via packet service. |
| 232 | */ |
| 233 | private void cancelArpPackets() { |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 234 | TrafficSelector.Builder selectorArpServer = DefaultTrafficSelector.builder() |
| 235 | .matchEthType(Ethernet.TYPE_ARP); |
| 236 | packetService.cancelPackets(selectorArpServer.build(), PacketPriority.CONTROL, appId); |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | private class DhcpRelayPacketProcessor implements PacketProcessor { |
| 240 | |
| 241 | @Override |
| 242 | public void process(PacketContext context) { |
| 243 | // process the packet and get the payload |
| 244 | Ethernet packet = context.inPacket().parsed(); |
| 245 | |
| 246 | if (packet == null) { |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | if (packet.getEtherType() == Ethernet.TYPE_IPV4) { |
| 251 | IPv4 ipv4Packet = (IPv4) packet.getPayload(); |
| 252 | |
| 253 | if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_UDP) { |
| 254 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 255 | DHCP dhcpPayload = (DHCP) udpPacket.getPayload(); |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 256 | if (udpPacket.getSourcePort() == UDP.DHCP_CLIENT_PORT || |
| 257 | udpPacket.getSourcePort() == UDP.DHCP_SERVER_PORT) { |
gaurav | 4af95fb | 2016-07-07 01:48:44 +0530 | [diff] [blame] | 258 | //This packet is dhcp. |
| 259 | processDhcpPacket(context, dhcpPayload); |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 260 | } |
| 261 | } |
Pier Luigi | 20557c8 | 2017-01-12 22:40:46 -0800 | [diff] [blame] | 262 | } else if (packet.getEtherType() == Ethernet.TYPE_ARP && arpEnabled) { |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 263 | ARP arpPacket = (ARP) packet.getPayload(); |
| 264 | Set<Interface> serverInterfaces = interfaceService. |
| 265 | getInterfacesByPort(context.inPacket().receivedFrom()); |
| 266 | //ignore the packets if dhcp server interface is not configured on onos. |
| 267 | if (serverInterfaces.isEmpty()) { |
| 268 | log.warn("server virtual interface not configured"); |
| 269 | return; |
| 270 | } |
| 271 | if ((arpPacket.getOpCode() == ARP.OP_REQUEST) && |
| 272 | checkArpRequestFrmDhcpServ(serverInterfaces, arpPacket)) { |
| 273 | processArpPacket(context, packet); |
| 274 | } |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 278 | //method to check the arp request is from dhcp server for default-gateway. |
| 279 | private boolean checkArpRequestFrmDhcpServ(Set<Interface> serverInterfaces, ARP arpPacket) { |
| 280 | if (Objects.equals(serverInterfaces.iterator().next().ipAddressesList().get(0). |
| 281 | ipAddress().getIp4Address(), |
| 282 | Ip4Address.valueOf(arpPacket.getTargetProtocolAddress()))) { |
| 283 | return true; |
| 284 | } |
| 285 | return false; |
| 286 | } |
| 287 | |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 288 | //forward the packet to ConnectPoint where the DHCP server is attached. |
gaurav | 4af95fb | 2016-07-07 01:48:44 +0530 | [diff] [blame] | 289 | private void forwardPacket(Ethernet packet) { |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 290 | |
| 291 | //send Packetout to dhcp server connectpoint. |
| 292 | if (dhcpServerConnectPoint != null) { |
| 293 | TrafficTreatment t = DefaultTrafficTreatment.builder() |
| 294 | .setOutput(dhcpServerConnectPoint.port()).build(); |
| 295 | OutboundPacket o = new DefaultOutboundPacket( |
gaurav | 4af95fb | 2016-07-07 01:48:44 +0530 | [diff] [blame] | 296 | dhcpServerConnectPoint.deviceId(), t, ByteBuffer.wrap(packet.serialize())); |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 297 | packetService.emit(o); |
| 298 | } |
| 299 | } |
| 300 | |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 301 | /** |
| 302 | * Processes the ARP Payload and initiates a reply to the client. |
| 303 | * |
| 304 | * @param context context of the incoming message |
| 305 | * @param packet the ethernet payload |
| 306 | */ |
| 307 | private void processArpPacket(PacketContext context, Ethernet packet) { |
| 308 | |
| 309 | ARP arpPacket = (ARP) packet.getPayload(); |
| 310 | |
| 311 | ARP arpReply = (ARP) arpPacket.clone(); |
| 312 | arpReply.setOpCode(ARP.OP_REPLY); |
| 313 | |
| 314 | arpReply.setTargetProtocolAddress(arpPacket.getSenderProtocolAddress()); |
| 315 | arpReply.setTargetHardwareAddress(arpPacket.getSenderHardwareAddress()); |
| 316 | arpReply.setSenderProtocolAddress(arpPacket.getTargetProtocolAddress()); |
| 317 | arpReply.setSenderHardwareAddress(myMAC.toBytes()); |
| 318 | |
| 319 | // Ethernet Frame. |
| 320 | Ethernet ethReply = new Ethernet(); |
| 321 | ethReply.setSourceMACAddress(myMAC); |
| 322 | ethReply.setDestinationMACAddress(packet.getSourceMAC()); |
| 323 | ethReply.setEtherType(Ethernet.TYPE_ARP); |
| 324 | ethReply.setVlanID(packet.getVlanID()); |
| 325 | |
| 326 | ethReply.setPayload(arpReply); |
| 327 | forwardPacket(ethReply); |
| 328 | } |
| 329 | |
gaurav | 4af95fb | 2016-07-07 01:48:44 +0530 | [diff] [blame] | 330 | //process the dhcp packet before sending to server |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 331 | private void processDhcpPacket(PacketContext context, DHCP dhcpPayload) { |
Charles Chan | 1b20123 | 2016-12-07 15:39:22 -0800 | [diff] [blame] | 332 | ConnectPoint inPort = context.inPacket().receivedFrom(); |
| 333 | Set<Interface> clientServerInterfaces = interfaceService.getInterfacesByPort(inPort); |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 334 | //ignore the packets if dhcp client interface is not configured on onos. |
| 335 | if (clientServerInterfaces.isEmpty()) { |
Charles Chan | 1b20123 | 2016-12-07 15:39:22 -0800 | [diff] [blame] | 336 | log.warn("Virtual interface is not configured on {}", inPort); |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 337 | return; |
| 338 | } |
| 339 | |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 340 | if (dhcpPayload == null) { |
| 341 | return; |
| 342 | } |
gaurav | 4af95fb | 2016-07-07 01:48:44 +0530 | [diff] [blame] | 343 | |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 344 | Ethernet packet = context.inPacket().parsed(); |
| 345 | DHCPPacketType incomingPacketType = null; |
| 346 | for (DHCPOption option : dhcpPayload.getOptions()) { |
| 347 | if (option.getCode() == OptionCode_MessageType.getValue()) { |
| 348 | byte[] data = option.getData(); |
| 349 | incomingPacketType = DHCPPacketType.getType(data[0]); |
| 350 | } |
| 351 | } |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 352 | |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 353 | switch (incomingPacketType) { |
| 354 | case DHCPDISCOVER: |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 355 | //add the gatewayip as virtual interface ip for server to understand the lease to be assigned |
| 356 | //and forward the packet to dhcp server. |
| 357 | Ethernet ethernetPacketDiscover = processDhcpPacketFrmClient(context, packet, clientServerInterfaces); |
gaurav | 4af95fb | 2016-07-07 01:48:44 +0530 | [diff] [blame] | 358 | forwardPacket(ethernetPacketDiscover); |
| 359 | break; |
| 360 | case DHCPOFFER: |
| 361 | //reply to dhcp client. |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 362 | Ethernet ethernetPacketOffer = processDhcpPacketFrmServer(packet); |
| 363 | sendReply(ethernetPacketOffer, dhcpPayload); |
gaurav | 4af95fb | 2016-07-07 01:48:44 +0530 | [diff] [blame] | 364 | break; |
| 365 | case DHCPREQUEST: |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 366 | //add the gatewayip as virtual interface ip for server to understand the lease to be assigned |
| 367 | //and forward the packet to dhcp server. |
| 368 | Ethernet ethernetPacketRequest = processDhcpPacketFrmClient(context, packet, clientServerInterfaces); |
gaurav | 4af95fb | 2016-07-07 01:48:44 +0530 | [diff] [blame] | 369 | forwardPacket(ethernetPacketRequest); |
| 370 | break; |
| 371 | case DHCPACK: |
| 372 | //reply to dhcp client. |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 373 | Ethernet ethernetPacketAck = processDhcpPacketFrmServer(packet); |
| 374 | sendReply(ethernetPacketAck, dhcpPayload); |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 375 | break; |
| 376 | default: |
| 377 | break; |
| 378 | } |
gaurav | 4af95fb | 2016-07-07 01:48:44 +0530 | [diff] [blame] | 379 | } |
| 380 | |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 381 | //build the DHCP discover/request packet with gatewayip(unicast packet) |
| 382 | private Ethernet processDhcpPacketFrmClient(PacketContext context, Ethernet ethernetPacket, |
| 383 | Set<Interface> clientInterfaces) { |
gaurav | 4af95fb | 2016-07-07 01:48:44 +0530 | [diff] [blame] | 384 | |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 385 | //assuming one interface per port for now. |
| 386 | relayAgentIP = clientInterfaces.iterator().next().ipAddressesList().get(0). |
| 387 | ipAddress().getIp4Address(); |
| 388 | relayAgentMAC = clientInterfaces.iterator().next().mac(); |
gaurav | 4af95fb | 2016-07-07 01:48:44 +0530 | [diff] [blame] | 389 | // get dhcp header. |
| 390 | Ethernet etherReply = (Ethernet) ethernetPacket.clone(); |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 391 | etherReply.setSourceMACAddress(relayAgentMAC); |
| 392 | etherReply.setDestinationMACAddress(dhcpServerMac); |
gaurav | 4af95fb | 2016-07-07 01:48:44 +0530 | [diff] [blame] | 393 | IPv4 ipv4Packet = (IPv4) etherReply.getPayload(); |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 394 | ipv4Packet.setSourceAddress(relayAgentIP.toInt()); |
| 395 | ipv4Packet.setDestinationAddress(dhcpServerIp.toInt()); |
gaurav | 4af95fb | 2016-07-07 01:48:44 +0530 | [diff] [blame] | 396 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 397 | DHCP dhcpPacket = (DHCP) udpPacket.getPayload(); |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 398 | dhcpPacket.setGatewayIPAddress(relayAgentIP.toInt()); |
gaurav | 4af95fb | 2016-07-07 01:48:44 +0530 | [diff] [blame] | 399 | udpPacket.setPayload(dhcpPacket); |
| 400 | ipv4Packet.setPayload(udpPacket); |
| 401 | etherReply.setPayload(ipv4Packet); |
| 402 | return etherReply; |
| 403 | } |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 404 | |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 405 | //build the DHCP offer/ack with proper client port. |
| 406 | private Ethernet processDhcpPacketFrmServer(Ethernet ethernetPacket) { |
gaurav | 4af95fb | 2016-07-07 01:48:44 +0530 | [diff] [blame] | 407 | |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 408 | // get dhcp header. |
| 409 | Ethernet etherReply = (Ethernet) ethernetPacket.clone(); |
| 410 | IPv4 ipv4Packet = (IPv4) etherReply.getPayload(); |
| 411 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 412 | DHCP dhcpPayload = (DHCP) udpPacket.getPayload(); |
| 413 | //set the ethernet frame. |
| 414 | etherReply.setDestinationMACAddress(dhcpPayload.getClientHardwareAddress()); |
| 415 | udpPacket.setDestinationPort(UDP.DHCP_CLIENT_PORT); |
Charles Chan | da5dc8c | 2017-01-13 14:29:29 -0800 | [diff] [blame] | 416 | // SRC_IP: relay agent IP |
| 417 | // DST_IP: offered IP |
| 418 | ipv4Packet.setSourceAddress(relayAgentIP.toInt()); |
| 419 | ipv4Packet.setDestinationAddress(dhcpPayload.getYourIPAddress()); |
| 420 | |
gaurav | 38351de | 2016-07-27 04:44:33 +0530 | [diff] [blame] | 421 | udpPacket.setPayload(dhcpPayload); |
| 422 | ipv4Packet.setPayload(udpPacket); |
| 423 | etherReply.setPayload(ipv4Packet); |
| 424 | return etherReply; |
| 425 | } |
| 426 | |
| 427 | //send the response to the requestor host. |
| 428 | private void sendReply(Ethernet ethPacket, DHCP dhcpPayload) { |
| 429 | |
| 430 | MacAddress descMac = new MacAddress(dhcpPayload.getClientHardwareAddress()); |
| 431 | Host host = hostService.getHost(HostId.hostId(descMac, |
gaurav | 4af95fb | 2016-07-07 01:48:44 +0530 | [diff] [blame] | 432 | VlanId.vlanId(ethPacket.getVlanID()))); |
Lei Xu | 3aeaf72 | 2016-08-11 13:51:12 -0700 | [diff] [blame] | 433 | //handle the concurrent host offline scenario |
| 434 | if (host == null) { |
| 435 | return; |
| 436 | } |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 437 | ConnectPoint dhcpRequestor = new ConnectPoint(host.location().elementId(), |
| 438 | host.location().port()); |
| 439 | |
| 440 | //send Packetout to requestor host. |
| 441 | if (dhcpRequestor != null) { |
| 442 | TrafficTreatment t = DefaultTrafficTreatment.builder() |
| 443 | .setOutput(dhcpRequestor.port()).build(); |
| 444 | OutboundPacket o = new DefaultOutboundPacket( |
gaurav | 4af95fb | 2016-07-07 01:48:44 +0530 | [diff] [blame] | 445 | dhcpRequestor.deviceId(), t, ByteBuffer.wrap(ethPacket.serialize())); |
gaurav | f088456 | 2016-06-17 02:47:13 +0530 | [diff] [blame] | 446 | packetService.emit(o); |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * Listener for network config events. |
| 453 | */ |
| 454 | private class InternalConfigListener implements NetworkConfigListener { |
| 455 | |
| 456 | @Override |
| 457 | public void event(NetworkConfigEvent event) { |
| 458 | |
| 459 | if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 460 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) && |
| 461 | event.configClass().equals(DhcpRelayConfig.class)) { |
| 462 | updateConfig(); |
| 463 | log.info("Reconfigured"); |
| 464 | } |
| 465 | } |
| 466 | } |
Lei Xu | 3aeaf72 | 2016-08-11 13:51:12 -0700 | [diff] [blame] | 467 | } |