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