Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 1 | /* |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [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.opencord.dhcpl2relay; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 21 | import com.google.common.collect.Sets; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [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; |
| 25 | import org.apache.felix.scr.annotations.Modified; |
| 26 | import org.apache.felix.scr.annotations.Property; |
| 27 | import org.apache.felix.scr.annotations.Reference; |
| 28 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 29 | import org.onlab.packet.DHCP; |
| 30 | import org.onlab.packet.DHCPOption; |
| 31 | import org.onlab.packet.DHCPPacketType; |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 32 | import org.onlab.packet.Ethernet; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 33 | import org.onlab.packet.IPv4; |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 34 | import org.onlab.packet.IpAddress; |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 35 | import org.onlab.packet.MacAddress; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 36 | import org.onlab.packet.TpPort; |
| 37 | import org.onlab.packet.UDP; |
| 38 | import org.onlab.packet.VlanId; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 39 | import org.onlab.util.Tools; |
| 40 | import org.onosproject.cfg.ComponentConfigService; |
| 41 | import org.onosproject.core.ApplicationId; |
| 42 | import org.onosproject.core.CoreService; |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 43 | import org.onosproject.mastership.MastershipEvent; |
| 44 | import org.onosproject.mastership.MastershipListener; |
| 45 | import org.onosproject.mastership.MastershipService; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 46 | import org.onosproject.net.AnnotationKeys; |
| 47 | import org.onosproject.net.ConnectPoint; |
| 48 | import org.onosproject.net.Host; |
| 49 | import org.onosproject.net.Port; |
| 50 | import org.onosproject.net.config.ConfigFactory; |
| 51 | import org.onosproject.net.config.NetworkConfigEvent; |
| 52 | import org.onosproject.net.config.NetworkConfigListener; |
| 53 | import org.onosproject.net.config.NetworkConfigRegistry; |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 54 | import org.onosproject.net.device.DeviceEvent; |
| 55 | import org.onosproject.net.device.DeviceListener; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 56 | import org.onosproject.net.device.DeviceService; |
| 57 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 58 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 59 | import org.onosproject.net.flow.TrafficSelector; |
| 60 | import org.onosproject.net.flow.TrafficTreatment; |
| 61 | import org.onosproject.net.host.HostService; |
| 62 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 63 | import org.onosproject.net.packet.OutboundPacket; |
| 64 | import org.onosproject.net.packet.PacketContext; |
| 65 | import org.onosproject.net.packet.PacketPriority; |
| 66 | import org.onosproject.net.packet.PacketProcessor; |
| 67 | import org.onosproject.net.packet.PacketService; |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 68 | import org.opencord.dhcpl2relay.packet.DhcpOption82; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 69 | import org.opencord.sadis.SubscriberAndDeviceInformation; |
| 70 | import org.opencord.sadis.SubscriberAndDeviceInformationService; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 71 | import org.osgi.service.component.ComponentContext; |
| 72 | import org.slf4j.Logger; |
| 73 | import org.slf4j.LoggerFactory; |
| 74 | |
| 75 | import java.nio.ByteBuffer; |
Amit Ghosh | 2095dc6 | 2017-09-25 20:56:55 +0100 | [diff] [blame^] | 76 | import java.util.Arrays; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 77 | import java.util.Dictionary; |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 78 | import java.util.List; |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 79 | import java.util.Map; |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 80 | import java.util.Set; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 81 | import java.util.concurrent.atomic.AtomicReference; |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 82 | import java.util.stream.Collectors; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 83 | |
| 84 | import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_MessageType; |
| 85 | import static org.onlab.packet.MacAddress.valueOf; |
| 86 | import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
| 87 | |
| 88 | /** |
| 89 | * DHCP Relay Agent Application Component. |
| 90 | */ |
| 91 | @Component(immediate = true) |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 92 | public class |
| 93 | DhcpL2Relay { |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 94 | |
| 95 | public static final String DHCP_L2RELAY_APP = "org.opencord.dhcpl2relay"; |
| 96 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 97 | private final InternalConfigListener cfgListener = |
| 98 | new InternalConfigListener(); |
| 99 | |
| 100 | private final Set<ConfigFactory> factories = ImmutableSet.of( |
| 101 | new ConfigFactory<ApplicationId, DhcpL2RelayConfig>(APP_SUBJECT_FACTORY, |
| 102 | DhcpL2RelayConfig.class, |
| 103 | "dhcpl2relay") { |
| 104 | @Override |
| 105 | public DhcpL2RelayConfig createConfig() { |
| 106 | return new DhcpL2RelayConfig(); |
| 107 | } |
| 108 | } |
| 109 | ); |
| 110 | |
| 111 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 112 | protected NetworkConfigRegistry cfgService; |
| 113 | |
| 114 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 115 | protected CoreService coreService; |
| 116 | |
| 117 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 118 | protected PacketService packetService; |
| 119 | |
| 120 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 121 | protected HostService hostService; |
| 122 | |
| 123 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 124 | protected ComponentConfigService componentConfigService; |
| 125 | |
| 126 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 127 | protected SubscriberAndDeviceInformationService subsService; |
| 128 | |
| 129 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 130 | protected DeviceService deviceService; |
| 131 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 132 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 133 | protected MastershipService mastershipService; |
| 134 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 135 | @Property(name = "option82", boolValue = true, |
| 136 | label = "Add option 82 to relayed packets") |
| 137 | protected boolean option82 = true; |
| 138 | |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 139 | @Property(name = "enableDhcpBroadcastReplies", boolValue = false, |
Amit Ghosh | 2095dc6 | 2017-09-25 20:56:55 +0100 | [diff] [blame^] | 140 | label = "Ask the DHCP Server to send back replies as L2 broadcast") |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 141 | protected boolean enableDhcpBroadcastReplies = false; |
| 142 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 143 | private DhcpRelayPacketProcessor dhcpRelayPacketProcessor = |
| 144 | new DhcpRelayPacketProcessor(); |
| 145 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 146 | private InnerMastershipListener changeListener = new InnerMastershipListener(); |
| 147 | private InnerDeviceListener deviceListener = new InnerDeviceListener(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 148 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 149 | // connect points to the DHCP server |
| 150 | Set<ConnectPoint> dhcpConnectPoints; |
| 151 | private AtomicReference<ConnectPoint> dhcpServerConnectPoint = new AtomicReference<>(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 152 | private MacAddress dhcpConnectMac = MacAddress.BROADCAST; |
| 153 | private ApplicationId appId; |
| 154 | |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 155 | static Map<String, DhcpAllocationInfo> allocationMap = Maps.newConcurrentMap(); |
| 156 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 157 | @Activate |
| 158 | protected void activate(ComponentContext context) { |
| 159 | //start the dhcp relay agent |
| 160 | appId = coreService.registerApplication(DHCP_L2RELAY_APP); |
| 161 | componentConfigService.registerProperties(getClass()); |
| 162 | |
| 163 | cfgService.addListener(cfgListener); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 164 | mastershipService.addListener(changeListener); |
| 165 | deviceService.addListener(deviceListener); |
| 166 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 167 | factories.forEach(cfgService::registerConfigFactory); |
| 168 | //update the dhcp server configuration. |
| 169 | updateConfig(); |
| 170 | //add the packet services. |
| 171 | packetService.addProcessor(dhcpRelayPacketProcessor, |
| 172 | PacketProcessor.director(0)); |
| 173 | requestDhcpPackets(); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 174 | if (context != null) { |
| 175 | modified(context); |
| 176 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 177 | |
| 178 | log.info("DHCP-L2-RELAY Started"); |
| 179 | } |
| 180 | |
| 181 | @Deactivate |
| 182 | protected void deactivate() { |
| 183 | cfgService.removeListener(cfgListener); |
| 184 | factories.forEach(cfgService::unregisterConfigFactory); |
| 185 | packetService.removeProcessor(dhcpRelayPacketProcessor); |
| 186 | cancelDhcpPackets(); |
| 187 | |
| 188 | componentConfigService.unregisterProperties(getClass(), false); |
Deepa Vaddireddy | 77a6ac7 | 2017-09-20 20:36:52 +0530 | [diff] [blame] | 189 | deviceService.removeListener(deviceListener); |
| 190 | mastershipService.removeListener(changeListener); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 191 | log.info("DHCP-L2-RELAY Stopped"); |
| 192 | } |
| 193 | |
| 194 | @Modified |
| 195 | protected void modified(ComponentContext context) { |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 196 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 197 | Dictionary<?, ?> properties = context.getProperties(); |
| 198 | |
| 199 | Boolean o = Tools.isPropertyEnabled(properties, "option82"); |
| 200 | if (o != null) { |
| 201 | option82 = o; |
| 202 | } |
Amit Ghosh | 2095dc6 | 2017-09-25 20:56:55 +0100 | [diff] [blame^] | 203 | |
| 204 | o = Tools.isPropertyEnabled(properties, "enableDhcpBroadcastReplies"); |
| 205 | if (o != null) { |
| 206 | enableDhcpBroadcastReplies = o; |
| 207 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Checks if this app has been configured. |
| 212 | * |
| 213 | * @return true if all information we need have been initialized |
| 214 | */ |
| 215 | private boolean configured() { |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 216 | return dhcpServerConnectPoint.get() != null; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 217 | } |
| 218 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 219 | /** |
| 220 | * Selects a connect point through an available device for which it is the master. |
| 221 | */ |
| 222 | private void selectServerConnectPoint() { |
| 223 | synchronized (this) { |
| 224 | dhcpServerConnectPoint.set(null); |
| 225 | if (dhcpConnectPoints != null) { |
| 226 | // find a connect point through a device for which we are master |
| 227 | for (ConnectPoint cp: dhcpConnectPoints) { |
| 228 | if (mastershipService.isLocalMaster(cp.deviceId())) { |
| 229 | if (deviceService.isAvailable(cp.deviceId())) { |
| 230 | dhcpServerConnectPoint.set(cp); |
| 231 | } |
| 232 | log.info("DHCP connectPoint selected is {}", cp); |
| 233 | break; |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | log.info("DHCP Server connectPoint is {}", dhcpServerConnectPoint.get()); |
| 239 | |
| 240 | if (dhcpServerConnectPoint.get() == null) { |
| 241 | log.error("Master of none, can't relay DHCP Message to server"); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Updates the network configuration. |
| 248 | */ |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 249 | private void updateConfig() { |
| 250 | DhcpL2RelayConfig cfg = cfgService.getConfig(appId, DhcpL2RelayConfig.class); |
| 251 | if (cfg == null) { |
| 252 | log.warn("Dhcp Server info not available"); |
| 253 | return; |
| 254 | } |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 255 | |
| 256 | dhcpConnectPoints = Sets.newConcurrentHashSet(cfg.getDhcpServerConnectPoint()); |
| 257 | |
| 258 | selectServerConnectPoint(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 259 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 260 | log.info("dhcp server connect point: " + dhcpServerConnectPoint); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Request DHCP packet in via PacketService. |
| 265 | */ |
| 266 | private void requestDhcpPackets() { |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 267 | TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder() |
| 268 | .matchEthType(Ethernet.TYPE_IPV4) |
| 269 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
| 270 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_SERVER_PORT)); |
| 271 | packetService.requestPackets(selectorServer.build(), |
| 272 | PacketPriority.CONTROL, appId); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 273 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 274 | TrafficSelector.Builder selectorClient = DefaultTrafficSelector.builder() |
| 275 | .matchEthType(Ethernet.TYPE_IPV4) |
| 276 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
| 277 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_CLIENT_PORT)); |
| 278 | packetService.requestPackets(selectorClient.build(), |
| 279 | PacketPriority.CONTROL, appId); |
| 280 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Cancel requested DHCP packets in via packet service. |
| 285 | */ |
| 286 | private void cancelDhcpPackets() { |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 287 | TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder() |
| 288 | .matchEthType(Ethernet.TYPE_IPV4) |
| 289 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
| 290 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_SERVER_PORT)); |
| 291 | packetService.cancelPackets(selectorServer.build(), |
| 292 | PacketPriority.CONTROL, appId); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 293 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 294 | TrafficSelector.Builder selectorClient = DefaultTrafficSelector.builder() |
| 295 | .matchEthType(Ethernet.TYPE_IPV4) |
| 296 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
| 297 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_CLIENT_PORT)); |
| 298 | packetService.cancelPackets(selectorClient.build(), |
| 299 | PacketPriority.CONTROL, appId); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 300 | } |
| 301 | |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 302 | public static Map<String, DhcpAllocationInfo> allocationMap() { |
| 303 | return allocationMap; |
| 304 | } |
| 305 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 306 | private SubscriberAndDeviceInformation getDevice(PacketContext context) { |
| 307 | String serialNo = deviceService.getDevice(context.inPacket(). |
| 308 | receivedFrom().deviceId()).serialNumber(); |
| 309 | |
| 310 | return subsService.get(serialNo); |
| 311 | } |
| 312 | |
| 313 | private SubscriberAndDeviceInformation getDevice(ConnectPoint cp) { |
| 314 | String serialNo = deviceService.getDevice(cp.deviceId()). |
| 315 | serialNumber(); |
| 316 | |
| 317 | return subsService.get(serialNo); |
| 318 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 319 | |
| 320 | private MacAddress relayAgentMacAddress(PacketContext context) { |
| 321 | |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 322 | SubscriberAndDeviceInformation device = this.getDevice(context); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 323 | if (device == null) { |
| 324 | log.warn("Device not found for {}", context.inPacket(). |
| 325 | receivedFrom()); |
| 326 | return null; |
| 327 | } |
| 328 | |
| 329 | return device.hardwareIdentifier(); |
| 330 | } |
| 331 | |
| 332 | private String nasPortId(PacketContext context) { |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 333 | return nasPortId(context.inPacket().receivedFrom()); |
| 334 | } |
| 335 | |
| 336 | private String nasPortId(ConnectPoint cp) { |
| 337 | Port p = deviceService.getPort(cp); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 338 | return p.annotations().value(AnnotationKeys.PORT_NAME); |
| 339 | } |
| 340 | |
| 341 | private SubscriberAndDeviceInformation getSubscriber(PacketContext context) { |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 342 | return subsService.get(nasPortId(context)); |
| 343 | } |
| 344 | |
| 345 | private VlanId cTag(PacketContext context) { |
| 346 | SubscriberAndDeviceInformation sub = getSubscriber(context); |
| 347 | if (sub == null) { |
| 348 | log.warn("Subscriber info not found for {}", context.inPacket(). |
| 349 | receivedFrom()); |
| 350 | return VlanId.NONE; |
| 351 | } |
| 352 | return sub.cTag(); |
| 353 | } |
| 354 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 355 | private VlanId cTag(ConnectPoint cp) { |
| 356 | String portId = nasPortId(cp); |
| 357 | SubscriberAndDeviceInformation sub = subsService.get(portId); |
| 358 | if (sub == null) { |
| 359 | log.warn("Subscriber info not found for {} looking for C-TAG", cp); |
| 360 | return VlanId.NONE; |
| 361 | } |
| 362 | return sub.cTag(); |
| 363 | } |
| 364 | |
| 365 | private VlanId sTag(ConnectPoint cp) { |
| 366 | String portId = nasPortId(cp); |
| 367 | SubscriberAndDeviceInformation sub = subsService.get(portId); |
| 368 | if (sub == null) { |
| 369 | log.warn("Subscriber info not found for {} looking for S-TAG", cp); |
| 370 | return VlanId.NONE; |
| 371 | } |
| 372 | return sub.sTag(); |
| 373 | } |
| 374 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 375 | private VlanId sTag(PacketContext context) { |
| 376 | SubscriberAndDeviceInformation sub = getSubscriber(context); |
| 377 | if (sub == null) { |
| 378 | log.warn("Subscriber info not found for {}", context.inPacket(). |
| 379 | receivedFrom()); |
| 380 | return VlanId.NONE; |
| 381 | } |
| 382 | return sub.sTag(); |
| 383 | } |
| 384 | |
| 385 | private class DhcpRelayPacketProcessor implements PacketProcessor { |
| 386 | |
| 387 | @Override |
| 388 | public void process(PacketContext context) { |
| 389 | if (!configured()) { |
| 390 | log.warn("Missing DHCP relay config. Abort packet processing"); |
| 391 | return; |
| 392 | } |
| 393 | |
| 394 | // process the packet and get the payload |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 395 | Ethernet packet = context.inPacket().parsed(); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 396 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 397 | if (packet == null) { |
| 398 | log.warn("Packet is null"); |
| 399 | return; |
| 400 | } |
| 401 | |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 402 | log.debug("Got a packet ", packet); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 403 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 404 | if (packet.getEtherType() == Ethernet.TYPE_IPV4) { |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 405 | IPv4 ipv4Packet = (IPv4) packet.getPayload(); |
| 406 | |
| 407 | if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_UDP) { |
| 408 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 409 | if (udpPacket.getSourcePort() == UDP.DHCP_CLIENT_PORT || |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 410 | udpPacket.getSourcePort() == UDP.DHCP_SERVER_PORT) { |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 411 | DHCP dhcpPayload = (DHCP) udpPacket.getPayload(); |
| 412 | //This packet is dhcp. |
| 413 | processDhcpPacket(context, packet, dhcpPayload); |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | //forward the packet to ConnectPoint where the DHCP server is attached. |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 420 | private void forwardPacket(Ethernet packet) { |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 421 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 422 | if (dhcpServerConnectPoint.get() != null) { |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 423 | TrafficTreatment t = DefaultTrafficTreatment.builder() |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 424 | .setOutput(dhcpServerConnectPoint.get().port()).build(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 425 | OutboundPacket o = new DefaultOutboundPacket( |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 426 | dhcpServerConnectPoint.get().deviceId(), t, |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 427 | ByteBuffer.wrap(packet.serialize())); |
| 428 | if (log.isTraceEnabled()) { |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 429 | log.trace("Relaying packet to dhcp server {} at {}", |
| 430 | packet, dhcpServerConnectPoint.get()); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 431 | } |
| 432 | packetService.emit(o); |
| 433 | } else { |
| 434 | log.warn("No dhcp server connect point"); |
| 435 | } |
| 436 | } |
| 437 | |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 438 | // get the type of the DHCP packet |
| 439 | private DHCPPacketType getDhcpPacketType(DHCP dhcpPayload) { |
| 440 | |
| 441 | for (DHCPOption option : dhcpPayload.getOptions()) { |
| 442 | if (option.getCode() == OptionCode_MessageType.getValue()) { |
| 443 | byte[] data = option.getData(); |
| 444 | return DHCPPacketType.getType(data[0]); |
| 445 | } |
| 446 | } |
| 447 | return null; |
| 448 | } |
| 449 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 450 | //process the dhcp packet before sending to server |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 451 | private void processDhcpPacket(PacketContext context, Ethernet packet, |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 452 | DHCP dhcpPayload) { |
| 453 | if (dhcpPayload == null) { |
| 454 | log.warn("DHCP payload is null"); |
| 455 | return; |
| 456 | } |
| 457 | |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 458 | DHCPPacketType incomingPacketType = getDhcpPacketType(dhcpPayload); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 459 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 460 | log.info("Received DHCP Packet of type {}", incomingPacketType); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 461 | |
| 462 | switch (incomingPacketType) { |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 463 | case DHCPDISCOVER: |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 464 | Ethernet ethernetPacketDiscover = |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 465 | processDhcpPacketFromClient(context, packet); |
| 466 | if (ethernetPacketDiscover != null) { |
| 467 | forwardPacket(ethernetPacketDiscover); |
| 468 | } |
| 469 | break; |
| 470 | case DHCPOFFER: |
| 471 | //reply to dhcp client. |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 472 | Ethernet ethernetPacketOffer = processDhcpPacketFromServer(packet); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 473 | if (ethernetPacketOffer != null) { |
| 474 | sendReply(ethernetPacketOffer, dhcpPayload); |
| 475 | } |
| 476 | break; |
| 477 | case DHCPREQUEST: |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 478 | Ethernet ethernetPacketRequest = |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 479 | processDhcpPacketFromClient(context, packet); |
| 480 | if (ethernetPacketRequest != null) { |
| 481 | forwardPacket(ethernetPacketRequest); |
| 482 | } |
| 483 | break; |
| 484 | case DHCPACK: |
| 485 | //reply to dhcp client. |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 486 | Ethernet ethernetPacketAck = processDhcpPacketFromServer(packet); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 487 | if (ethernetPacketAck != null) { |
| 488 | sendReply(ethernetPacketAck, dhcpPayload); |
| 489 | } |
| 490 | break; |
| 491 | default: |
| 492 | break; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 496 | private Ethernet processDhcpPacketFromClient(PacketContext context, |
| 497 | Ethernet ethernetPacket) { |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 498 | |
| 499 | MacAddress relayAgentMac = relayAgentMacAddress(context); |
| 500 | if (relayAgentMac == null) { |
| 501 | log.warn("RelayAgent MAC not found "); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 502 | return null; |
| 503 | } |
| 504 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 505 | Ethernet etherReply = ethernetPacket; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 506 | |
| 507 | IPv4 ipv4Packet = (IPv4) etherReply.getPayload(); |
| 508 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 509 | DHCP dhcpPacket = (DHCP) udpPacket.getPayload(); |
| 510 | |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 511 | if (enableDhcpBroadcastReplies) { |
| 512 | // We want the reply to come back as a L2 broadcast |
| 513 | dhcpPacket.setFlags((short) 0x8000); |
| 514 | } |
| 515 | |
| 516 | // remove from the allocation map (used for display) as it's is |
| 517 | // requesting a fresh allocation |
| 518 | if (getDhcpPacketType(dhcpPacket) == DHCPPacketType.DHCPREQUEST) { |
| 519 | |
| 520 | String portId = nasPortId(context.inPacket().receivedFrom()); |
| 521 | SubscriberAndDeviceInformation sub = subsService.get(portId); |
| 522 | if (sub != null) { |
| 523 | allocationMap.remove(sub.nasPortId()); |
| 524 | } |
| 525 | } // end allocation for display |
| 526 | |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 527 | if (option82) { |
| 528 | SubscriberAndDeviceInformation entry = getSubscriber(context); |
| 529 | if (entry == null) { |
| 530 | log.info("Dropping packet as subscriber entry is not available"); |
| 531 | return null; |
| 532 | } |
| 533 | |
| 534 | DHCP dhcpPacketWithOption82 = addOption82(dhcpPacket, entry); |
| 535 | udpPacket.setPayload(dhcpPacketWithOption82); |
| 536 | } |
| 537 | |
| 538 | ipv4Packet.setPayload(udpPacket); |
| 539 | etherReply.setPayload(ipv4Packet); |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 540 | etherReply.setSourceMACAddress(relayAgentMac); |
| 541 | etherReply.setDestinationMACAddress(dhcpConnectMac); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 542 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 543 | etherReply.setPriorityCode(ethernetPacket.getPriorityCode()); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 544 | etherReply.setVlanID(cTag(context).toShort()); |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 545 | etherReply.setQinQTPID(Ethernet.TYPE_VLAN); |
| 546 | etherReply.setQinQVID(sTag(context).toShort()); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 547 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 548 | log.info("Finished processing packet -- sending packet {}", etherReply); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 549 | return etherReply; |
| 550 | } |
| 551 | |
| 552 | //build the DHCP offer/ack with proper client port. |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 553 | private Ethernet processDhcpPacketFromServer(Ethernet ethernetPacket) { |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 554 | // get dhcp header. |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 555 | Ethernet etherReply = (Ethernet) ethernetPacket.clone(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 556 | IPv4 ipv4Packet = (IPv4) etherReply.getPayload(); |
| 557 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 558 | DHCP dhcpPayload = (DHCP) udpPacket.getPayload(); |
| 559 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 560 | MacAddress dstMac = valueOf(dhcpPayload.getClientHardwareAddress()); |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 561 | ConnectPoint subsCp = getConnectPointOfClient(dstMac); |
Amit Ghosh | 2095dc6 | 2017-09-25 20:56:55 +0100 | [diff] [blame^] | 562 | // If we can't find the subscriber, can't process further |
| 563 | if (subsCp == null) { |
| 564 | return null; |
| 565 | } |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 566 | // if it's an ACK packet store the information for display purpose |
| 567 | if (getDhcpPacketType(dhcpPayload) == DHCPPacketType.DHCPACK) { |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 568 | |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 569 | String portId = nasPortId(subsCp); |
| 570 | SubscriberAndDeviceInformation sub = subsService.get(portId); |
| 571 | if (sub != null) { |
| 572 | List<DHCPOption> options = dhcpPayload.getOptions(); |
| 573 | List<DHCPOption> circuitIds = options.stream() |
| 574 | .filter(option -> option.getCode() == DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue()) |
| 575 | .collect(Collectors.toList()); |
| 576 | |
| 577 | String circuitId = "None"; |
| 578 | if (circuitIds.size() == 1) { |
Amit Ghosh | 2095dc6 | 2017-09-25 20:56:55 +0100 | [diff] [blame^] | 579 | byte[] array = circuitIds.get(0).getData(); |
| 580 | |
| 581 | try { |
| 582 | // we leave the first two bytes as they are the id and length |
| 583 | circuitId = new String(Arrays.copyOfRange(array, 2, array.length), "UTF-8"); |
| 584 | } catch (Exception e) { } |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | IpAddress ip = IpAddress.valueOf(dhcpPayload.getYourIPAddress()); |
| 588 | |
| 589 | //storeDHCPAllocationInfo |
| 590 | DhcpAllocationInfo info = new DhcpAllocationInfo(circuitId, dstMac, ip); |
| 591 | |
| 592 | allocationMap.put(sub.nasPortId(), info); |
| 593 | } |
| 594 | } // end storing of info |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 595 | |
| 596 | // we leave the srcMac from the original packet |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 597 | etherReply.setDestinationMACAddress(dstMac); |
| 598 | etherReply.setQinQVID(sTag(subsCp).toShort()); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 599 | etherReply.setPriorityCode(ethernetPacket.getPriorityCode()); |
| 600 | etherReply.setVlanID((cTag(subsCp).toShort())); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 601 | |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 602 | if (option82) { |
| 603 | udpPacket.setPayload(removeOption82(dhcpPayload)); |
| 604 | } else { |
| 605 | udpPacket.setPayload(dhcpPayload); |
| 606 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 607 | ipv4Packet.setPayload(udpPacket); |
| 608 | etherReply.setPayload(ipv4Packet); |
| 609 | |
| 610 | log.info("Finished processing packet"); |
| 611 | return etherReply; |
| 612 | } |
| 613 | |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 614 | /* |
| 615 | * Get ConnectPoint of the Client based on it's MAC address |
| 616 | */ |
| 617 | private ConnectPoint getConnectPointOfClient(MacAddress dstMac) { |
| 618 | Set<Host> hosts = hostService.getHostsByMac(dstMac); |
| 619 | if (hosts == null || hosts.isEmpty()) { |
| 620 | log.warn("Cannot determine host for DHCP client: {}. Aborting " |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 621 | + "relay for dhcp packet from server", |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 622 | dstMac); |
| 623 | return null; |
| 624 | } |
| 625 | for (Host h : hosts) { |
| 626 | // if more than one, |
| 627 | // find the connect point which has an valid entry in SADIS |
| 628 | ConnectPoint cp = new ConnectPoint(h.location().deviceId(), |
| 629 | h.location().port()); |
| 630 | |
| 631 | if (sTag(cp) != VlanId.NONE) { |
| 632 | return cp; |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | return null; |
| 637 | } |
| 638 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 639 | //send the response to the requester host. |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 640 | private void sendReply(Ethernet ethPacket, DHCP dhcpPayload) { |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 641 | MacAddress descMac = valueOf(dhcpPayload.getClientHardwareAddress()); |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 642 | ConnectPoint subCp = getConnectPointOfClient(descMac); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 643 | |
| 644 | // Send packet out to requester if the host information is available |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 645 | if (subCp != null) { |
| 646 | log.info("Sending DHCP packet to cp: {}", subCp); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 647 | TrafficTreatment t = DefaultTrafficTreatment.builder() |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 648 | .setOutput(subCp.port()).build(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 649 | OutboundPacket o = new DefaultOutboundPacket( |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 650 | subCp.deviceId(), t, ByteBuffer.wrap(ethPacket.serialize())); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 651 | if (log.isTraceEnabled()) { |
| 652 | log.trace("Relaying packet to dhcp client {}", ethPacket); |
| 653 | } |
| 654 | packetService.emit(o); |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 655 | log.info("DHCP Packet sent to {}", subCp); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 656 | } else { |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 657 | log.error("Dropping DHCP packet because can't find host for {}", descMac); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | } |
| 661 | |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 662 | private DHCP addOption82(DHCP dhcpPacket, SubscriberAndDeviceInformation entry) { |
| 663 | log.debug("option82data {} ", entry); |
| 664 | |
| 665 | List<DHCPOption> options = Lists.newArrayList(dhcpPacket.getOptions()); |
| 666 | DhcpOption82 option82 = new DhcpOption82(); |
| 667 | option82.setAgentCircuitId(entry.circuitId()); |
| 668 | option82.setAgentRemoteId(entry.remoteId()); |
| 669 | DHCPOption option = new DHCPOption() |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 670 | .setCode(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue()) |
| 671 | .setData(option82.toByteArray()) |
| 672 | .setLength(option82.length()); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 673 | |
| 674 | options.add(options.size() - 1, option); |
| 675 | dhcpPacket.setOptions(options); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 676 | |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 677 | return dhcpPacket; |
| 678 | |
| 679 | } |
| 680 | |
| 681 | private DHCP removeOption82(DHCP dhcpPacket) { |
| 682 | List<DHCPOption> options = dhcpPacket.getOptions(); |
| 683 | List<DHCPOption> newoptions = options.stream() |
| 684 | .filter(option -> option.getCode() != DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue()) |
| 685 | .collect(Collectors.toList()); |
| 686 | |
| 687 | return dhcpPacket.setOptions(newoptions); |
| 688 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 689 | /** |
| 690 | * Listener for network config events. |
| 691 | */ |
| 692 | private class InternalConfigListener implements NetworkConfigListener { |
| 693 | |
| 694 | @Override |
| 695 | public void event(NetworkConfigEvent event) { |
| 696 | |
| 697 | if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 698 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) && |
| 699 | event.configClass().equals(DhcpL2RelayConfig.class)) { |
| 700 | updateConfig(); |
| 701 | log.info("Reconfigured"); |
| 702 | } |
| 703 | } |
| 704 | } |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 705 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 706 | /** |
| 707 | * Handles Mastership changes for the devices which connect |
| 708 | * to the DHCP server. |
| 709 | */ |
| 710 | private class InnerMastershipListener implements MastershipListener { |
| 711 | @Override |
| 712 | public void event(MastershipEvent event) { |
| 713 | if (dhcpServerConnectPoint.get() != null && |
| 714 | dhcpServerConnectPoint.get().deviceId(). |
| 715 | equals(event.subject())) { |
| 716 | log.trace("Mastership Event recevived for {}", event.subject()); |
| 717 | // mastership of the device for our connect point has changed |
| 718 | // reselect |
| 719 | selectServerConnectPoint(); |
| 720 | } |
| 721 | } |
| 722 | } |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 723 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 724 | /** |
| 725 | * Handles Device status change for the devices which connect |
| 726 | * to the DHCP server. |
| 727 | */ |
| 728 | private class InnerDeviceListener implements DeviceListener { |
| 729 | @Override |
| 730 | public void event(DeviceEvent event) { |
| 731 | log.trace("Device Event recevived for {} event {}", event.subject(), event.type()); |
| 732 | if (dhcpServerConnectPoint.get() == null) { |
| 733 | switch (event.type()) { |
| 734 | case DEVICE_ADDED: |
| 735 | case DEVICE_AVAILABILITY_CHANGED: |
| 736 | // some device is available check if we can get one |
| 737 | selectServerConnectPoint(); |
| 738 | break; |
| 739 | default: |
| 740 | break; |
| 741 | } |
| 742 | return; |
| 743 | } |
| 744 | if (dhcpServerConnectPoint.get().deviceId(). |
| 745 | equals(event.subject().id())) { |
| 746 | switch (event.type()) { |
| 747 | case DEVICE_AVAILABILITY_CHANGED: |
| 748 | case DEVICE_REMOVED: |
| 749 | case DEVICE_SUSPENDED: |
| 750 | // state of our device has changed, check if we need |
| 751 | // to re-select |
| 752 | selectServerConnectPoint(); |
| 753 | break; |
| 754 | default: |
| 755 | break; |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 760 | } |