Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Ray Milkey | b65d784 | 2017-08-03 16:28:24 -0700 | [diff] [blame] | 17 | package org.onosproject.net.neighbour.impl; |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 18 | |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 19 | import com.google.common.collect.HashMultimap; |
| 20 | import com.google.common.collect.ImmutableMap; |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Multimaps; |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 22 | import com.google.common.collect.SetMultimap; |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Activate; |
| 24 | import org.apache.felix.scr.annotations.Component; |
| 25 | import org.apache.felix.scr.annotations.Deactivate; |
| 26 | import org.apache.felix.scr.annotations.Modified; |
| 27 | import org.apache.felix.scr.annotations.Property; |
| 28 | import org.apache.felix.scr.annotations.Reference; |
| 29 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 30 | import org.apache.felix.scr.annotations.Service; |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 31 | import org.onlab.packet.Ethernet; |
| 32 | import org.onlab.packet.ICMP6; |
| 33 | import org.onlab.packet.IPv6; |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 34 | import org.onlab.packet.IpAddress; |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 35 | import org.onlab.packet.MacAddress; |
| 36 | import org.onlab.packet.VlanId; |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 37 | import org.onlab.util.Tools; |
| 38 | import org.onosproject.cfg.ComponentConfigService; |
| 39 | import org.onosproject.core.ApplicationId; |
| 40 | import org.onosproject.core.CoreService; |
Ray Milkey | facf286 | 2017-08-03 11:58:29 -0700 | [diff] [blame] | 41 | import org.onosproject.net.intf.Interface; |
Ray Milkey | b65d784 | 2017-08-03 16:28:24 -0700 | [diff] [blame] | 42 | import org.onosproject.net.neighbour.NeighbourHandlerRegistration; |
| 43 | import org.onosproject.net.neighbour.NeighbourMessageActions; |
| 44 | import org.onosproject.net.neighbour.NeighbourMessageContext; |
| 45 | import org.onosproject.net.neighbour.NeighbourMessageHandler; |
| 46 | import org.onosproject.net.neighbour.NeighbourResolutionService; |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 47 | import org.onosproject.net.ConnectPoint; |
| 48 | import org.onosproject.net.edge.EdgePortService; |
| 49 | import org.onosproject.net.flow.DefaultTrafficSelector; |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 50 | import org.onosproject.net.flow.TrafficSelector; |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 51 | import org.onosproject.net.host.HostService; |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 52 | import org.onosproject.net.packet.InboundPacket; |
| 53 | import org.onosproject.net.packet.PacketContext; |
| 54 | import org.onosproject.net.packet.PacketProcessor; |
| 55 | import org.onosproject.net.packet.PacketService; |
| 56 | import org.osgi.service.component.ComponentContext; |
| 57 | import org.slf4j.Logger; |
| 58 | import org.slf4j.LoggerFactory; |
| 59 | |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 60 | import java.util.Collection; |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 61 | import java.util.Dictionary; |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 62 | import java.util.Iterator; |
| 63 | import java.util.Map; |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 64 | import java.util.Objects; |
Pier Ventre | 0ba9852 | 2016-09-19 15:49:14 -0700 | [diff] [blame] | 65 | import java.util.stream.Collectors; |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 66 | |
| 67 | import static com.google.common.base.Preconditions.checkNotNull; |
| 68 | import static org.onlab.packet.Ethernet.TYPE_ARP; |
| 69 | import static org.onlab.packet.Ethernet.TYPE_IPV6; |
| 70 | import static org.onlab.packet.ICMP6.NEIGHBOR_ADVERTISEMENT; |
| 71 | import static org.onlab.packet.ICMP6.NEIGHBOR_SOLICITATION; |
| 72 | import static org.onlab.packet.IPv6.PROTOCOL_ICMP6; |
| 73 | import static org.onosproject.net.packet.PacketPriority.CONTROL; |
| 74 | |
| 75 | /** |
| 76 | * Manages handlers for neighbour messages. |
| 77 | */ |
| 78 | @Service |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 79 | @Component(immediate = true) |
Jonathan Hart | 1e393bb | 2016-09-14 08:51:09 -0700 | [diff] [blame] | 80 | public class NeighbourResolutionManager implements NeighbourResolutionService { |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 81 | |
| 82 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 83 | |
| 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 85 | protected CoreService coreService; |
| 86 | |
| 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 88 | protected HostService hostService; |
| 89 | |
| 90 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 91 | protected EdgePortService edgeService; |
| 92 | |
| 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 94 | protected PacketService packetService; |
| 95 | |
| 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 97 | protected ComponentConfigService componentConfigService; |
| 98 | |
Pier Luigi | 65a0ab3 | 2017-01-12 20:52:38 -0800 | [diff] [blame] | 99 | @Property(name = "arpEnabled", boolValue = true, |
| 100 | label = "Enable Address resolution protocol") |
| 101 | protected boolean arpEnabled = true; |
| 102 | |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 103 | @Property(name = "ndpEnabled", boolValue = false, |
| 104 | label = "Enable IPv6 neighbour discovery") |
| 105 | protected boolean ndpEnabled = false; |
| 106 | |
Pier Luigi | 65a0ab3 | 2017-01-12 20:52:38 -0800 | [diff] [blame] | 107 | @Property(name = "requestInterceptsEnabled", boolValue = true, |
| 108 | label = "Enable requesting packet intercepts") |
| 109 | private boolean requestInterceptsEnabled = true; |
| 110 | |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 111 | private static final String APP_NAME = "org.onosproject.neighbour"; |
| 112 | private ApplicationId appId; |
| 113 | |
Jonathan Hart | c004adf | 2016-09-15 16:50:04 -0700 | [diff] [blame] | 114 | private final SetMultimap<ConnectPoint, NeighbourHandlerRegistration> packetHandlers = |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 115 | Multimaps.synchronizedSetMultimap(HashMultimap.create()); |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 116 | |
| 117 | private final InternalPacketProcessor processor = new InternalPacketProcessor(); |
Jonathan Hart | 584ea2d | 2016-10-11 10:49:16 +0200 | [diff] [blame] | 118 | private NeighbourMessageActions actions; |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 119 | |
| 120 | @Activate |
| 121 | protected void activate(ComponentContext context) { |
| 122 | appId = coreService.registerApplication(APP_NAME); |
| 123 | |
| 124 | componentConfigService.registerProperties(getClass()); |
| 125 | modified(context); |
| 126 | |
Jonathan Hart | 584ea2d | 2016-10-11 10:49:16 +0200 | [diff] [blame] | 127 | actions = new DefaultNeighbourMessageActions(packetService, edgeService); |
| 128 | |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 129 | packetService.addProcessor(processor, PacketProcessor.director(1)); |
| 130 | } |
| 131 | |
| 132 | @Deactivate |
| 133 | protected void deactivate() { |
| 134 | cancelPackets(); |
| 135 | packetService.removeProcessor(processor); |
| 136 | componentConfigService.unregisterProperties(getClass(), false); |
| 137 | } |
| 138 | |
| 139 | @Modified |
| 140 | protected void modified(ComponentContext context) { |
| 141 | Dictionary<?, ?> properties = context.getProperties(); |
| 142 | Boolean flag; |
| 143 | |
| 144 | flag = Tools.isPropertyEnabled(properties, "ndpEnabled"); |
| 145 | if (flag != null) { |
| 146 | ndpEnabled = flag; |
| 147 | log.info("IPv6 neighbor discovery is {}", |
| 148 | ndpEnabled ? "enabled" : "disabled"); |
| 149 | } |
| 150 | |
Pier Luigi | 65a0ab3 | 2017-01-12 20:52:38 -0800 | [diff] [blame] | 151 | flag = Tools.isPropertyEnabled(properties, "arpEnabled"); |
| 152 | if (flag != null) { |
| 153 | arpEnabled = flag; |
| 154 | log.info("Address resolution protocol is {}", |
| 155 | arpEnabled ? "enabled" : "disabled"); |
| 156 | } |
| 157 | |
| 158 | flag = Tools.isPropertyEnabled(properties, "requestInterceptsEnabled"); |
| 159 | if (flag == null) { |
| 160 | log.info("Request intercepts is not configured, " + |
| 161 | "using current value of {}", requestInterceptsEnabled); |
| 162 | } else { |
| 163 | requestInterceptsEnabled = flag; |
| 164 | log.info("Configured. Request intercepts is {}", |
| 165 | requestInterceptsEnabled ? "enabled" : "disabled"); |
| 166 | } |
| 167 | |
Jonathan Hart | c004adf | 2016-09-15 16:50:04 -0700 | [diff] [blame] | 168 | synchronized (packetHandlers) { |
Pier Luigi | 65a0ab3 | 2017-01-12 20:52:38 -0800 | [diff] [blame] | 169 | if (!packetHandlers.isEmpty() && requestInterceptsEnabled) { |
Jonathan Hart | c004adf | 2016-09-15 16:50:04 -0700 | [diff] [blame] | 170 | requestPackets(); |
Pier Luigi | 65a0ab3 | 2017-01-12 20:52:38 -0800 | [diff] [blame] | 171 | } else { |
| 172 | cancelPackets(); |
Jonathan Hart | c004adf | 2016-09-15 16:50:04 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | private void requestPackets() { |
Pier Luigi | 65a0ab3 | 2017-01-12 20:52:38 -0800 | [diff] [blame] | 178 | if (arpEnabled) { |
| 179 | packetService.requestPackets(buildArpSelector(), CONTROL, appId); |
| 180 | } else { |
| 181 | packetService.cancelPackets(buildArpSelector(), CONTROL, appId); |
| 182 | } |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 183 | |
| 184 | if (ndpEnabled) { |
| 185 | packetService.requestPackets(buildNeighborSolicitationSelector(), |
| 186 | CONTROL, appId); |
| 187 | packetService.requestPackets(buildNeighborAdvertisementSelector(), |
| 188 | CONTROL, appId); |
| 189 | } else { |
| 190 | packetService.cancelPackets(buildNeighborSolicitationSelector(), |
| 191 | CONTROL, appId); |
| 192 | packetService.cancelPackets(buildNeighborAdvertisementSelector(), |
| 193 | CONTROL, appId); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | private void cancelPackets() { |
| 198 | packetService.cancelPackets(buildArpSelector(), CONTROL, appId); |
| 199 | packetService.cancelPackets(buildNeighborSolicitationSelector(), |
| 200 | CONTROL, appId); |
| 201 | packetService.cancelPackets(buildNeighborAdvertisementSelector(), |
| 202 | CONTROL, appId); |
| 203 | } |
| 204 | |
| 205 | private TrafficSelector buildArpSelector() { |
| 206 | return DefaultTrafficSelector.builder() |
| 207 | .matchEthType(TYPE_ARP) |
| 208 | .build(); |
| 209 | } |
| 210 | |
| 211 | private TrafficSelector buildNeighborSolicitationSelector() { |
| 212 | return DefaultTrafficSelector.builder() |
| 213 | .matchEthType(TYPE_IPV6) |
| 214 | .matchIPProtocol(PROTOCOL_ICMP6) |
| 215 | .matchIcmpv6Type(NEIGHBOR_SOLICITATION) |
| 216 | .build(); |
| 217 | } |
| 218 | |
| 219 | private TrafficSelector buildNeighborAdvertisementSelector() { |
| 220 | return DefaultTrafficSelector.builder() |
| 221 | .matchEthType(TYPE_IPV6) |
| 222 | .matchIPProtocol(PROTOCOL_ICMP6) |
| 223 | .matchIcmpv6Type(NEIGHBOR_ADVERTISEMENT) |
| 224 | .build(); |
| 225 | } |
| 226 | |
| 227 | @Override |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 228 | public void registerNeighbourHandler(ConnectPoint connectPoint, |
| 229 | NeighbourMessageHandler handler, |
| 230 | ApplicationId appId) { |
Jonathan Hart | c004adf | 2016-09-15 16:50:04 -0700 | [diff] [blame] | 231 | register(connectPoint, new HandlerRegistration(handler, appId)); |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | @Override |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 235 | public void registerNeighbourHandler(Interface intf, |
| 236 | NeighbourMessageHandler handler, |
| 237 | ApplicationId appId) { |
Jonathan Hart | c004adf | 2016-09-15 16:50:04 -0700 | [diff] [blame] | 238 | register(intf.connectPoint(), new HandlerRegistration(handler, intf, appId)); |
| 239 | } |
| 240 | |
| 241 | private void register(ConnectPoint connectPoint, HandlerRegistration registration) { |
| 242 | synchronized (packetHandlers) { |
Pier Ventre | 09f88b8 | 2017-01-09 23:01:05 -0800 | [diff] [blame] | 243 | if (packetHandlers.isEmpty() && requestInterceptsEnabled) { |
Jonathan Hart | c004adf | 2016-09-15 16:50:04 -0700 | [diff] [blame] | 244 | requestPackets(); |
| 245 | } |
| 246 | packetHandlers.put(connectPoint, registration); |
| 247 | } |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | @Override |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 251 | public void unregisterNeighbourHandler(ConnectPoint connectPoint, |
| 252 | NeighbourMessageHandler handler, |
| 253 | ApplicationId appId) { |
Jonathan Hart | c004adf | 2016-09-15 16:50:04 -0700 | [diff] [blame] | 254 | unregister(connectPoint, new HandlerRegistration(handler, appId)); |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | @Override |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 258 | public void unregisterNeighbourHandler(Interface intf, |
| 259 | NeighbourMessageHandler handler, |
| 260 | ApplicationId appId) { |
Jonathan Hart | c004adf | 2016-09-15 16:50:04 -0700 | [diff] [blame] | 261 | unregister(intf.connectPoint(), new HandlerRegistration(handler, intf, appId)); |
| 262 | } |
| 263 | |
| 264 | private void unregister(ConnectPoint connectPoint, HandlerRegistration registration) { |
| 265 | synchronized (packetHandlers) { |
| 266 | packetHandlers.remove(connectPoint, registration); |
| 267 | |
| 268 | if (packetHandlers.isEmpty()) { |
| 269 | cancelPackets(); |
| 270 | } |
| 271 | } |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public void unregisterNeighbourHandlers(ApplicationId appId) { |
| 276 | synchronized (packetHandlers) { |
| 277 | Iterator<NeighbourHandlerRegistration> it = packetHandlers.values().iterator(); |
| 278 | |
| 279 | while (it.hasNext()) { |
| 280 | NeighbourHandlerRegistration registration = it.next(); |
| 281 | if (registration.appId().equals(appId)) { |
| 282 | it.remove(); |
| 283 | } |
| 284 | } |
Jonathan Hart | c004adf | 2016-09-15 16:50:04 -0700 | [diff] [blame] | 285 | |
| 286 | if (packetHandlers.isEmpty()) { |
| 287 | cancelPackets(); |
| 288 | } |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
| 292 | @Override |
| 293 | public Map<ConnectPoint, Collection<NeighbourHandlerRegistration>> getHandlerRegistrations() { |
| 294 | return ImmutableMap.copyOf(Multimaps.asMap(packetHandlers)); |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Jonathan Hart | 584ea2d | 2016-10-11 10:49:16 +0200 | [diff] [blame] | 297 | private void handlePacket(PacketContext context) { |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 298 | InboundPacket pkt = context.inPacket(); |
| 299 | Ethernet ethPkt = pkt.parsed(); |
| 300 | |
| 301 | NeighbourMessageContext msgContext = |
| 302 | DefaultNeighbourMessageContext.createContext(ethPkt, pkt.receivedFrom(), actions); |
| 303 | |
| 304 | if (msgContext == null) { |
| 305 | return; |
| 306 | } |
| 307 | |
Pier Ventre | 0ba9852 | 2016-09-19 15:49:14 -0700 | [diff] [blame] | 308 | if (handleMessage(msgContext)) { |
| 309 | context.block(); |
| 310 | } |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 311 | |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Pier Ventre | 0ba9852 | 2016-09-19 15:49:14 -0700 | [diff] [blame] | 314 | private boolean handleMessage(NeighbourMessageContext context) { |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 315 | Collection<NeighbourHandlerRegistration> handlers = packetHandlers.get(context.inPort()); |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 316 | |
Pier Ventre | 0ba9852 | 2016-09-19 15:49:14 -0700 | [diff] [blame] | 317 | Collection<NeighbourHandlerRegistration> handled = handlers |
| 318 | .stream() |
| 319 | .filter(registration -> registration.intf() == null || matches(context, registration.intf())) |
| 320 | .collect(Collectors.toSet()); |
| 321 | |
| 322 | handled.forEach(registration -> registration.handler().handleMessage(context, hostService)); |
| 323 | |
| 324 | return !handled.isEmpty(); |
| 325 | |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 328 | /** |
| 329 | * Checks that incoming packet matches the parameters of the interface. |
| 330 | * This means that if the interface specifies a particular parameter |
| 331 | * (VLAN, IP address, etc.) then the incoming packet should match those |
| 332 | * parameters. |
| 333 | * |
| 334 | * @param context incoming message context |
| 335 | * @param intf interface to check |
| 336 | * @return true if the incoming message matches the interface, otherwise false |
| 337 | */ |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 338 | private boolean matches(NeighbourMessageContext context, Interface intf) { |
| 339 | checkNotNull(context); |
| 340 | checkNotNull(intf); |
| 341 | |
| 342 | boolean matches = true; |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 343 | // For non-broadcast packets, if the interface has a MAC address check that |
| 344 | // the destination MAC address of the packet matches the interface MAC |
| 345 | if (!context.dstMac().isBroadcast() && |
| 346 | !intf.mac().equals(MacAddress.NONE) && |
| 347 | !intf.mac().equals(context.dstMac())) { |
| 348 | matches = false; |
| 349 | } |
| 350 | // If the interface has a VLAN, check that the packet's VLAN matches |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 351 | if (!intf.vlan().equals(VlanId.NONE) && !intf.vlan().equals(context.vlan())) { |
| 352 | matches = false; |
| 353 | } |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 354 | // If the interface has IP addresses, check that the packet's target IP |
| 355 | // address matches one of the interface IP addresses |
| 356 | if (!intf.ipAddressesList().isEmpty() && !hasIp(intf, context.target())) { |
| 357 | matches = false; |
| 358 | } |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 359 | |
| 360 | return matches; |
| 361 | } |
| 362 | |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 363 | /** |
| 364 | * Returns true if the interface has the given IP address. |
| 365 | * |
| 366 | * @param intf interface to check |
| 367 | * @param ip IP address |
| 368 | * @return true if the IP is configured on the interface, otherwise false |
| 369 | */ |
| 370 | private boolean hasIp(Interface intf, IpAddress ip) { |
| 371 | return intf.ipAddressesList().stream() |
| 372 | .anyMatch(intfAddress -> intfAddress.ipAddress().equals(ip)); |
| 373 | } |
| 374 | |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 375 | /** |
| 376 | * Stores a neighbour message handler registration. |
| 377 | */ |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 378 | private class HandlerRegistration implements NeighbourHandlerRegistration { |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 379 | private final Interface intf; |
| 380 | private final NeighbourMessageHandler handler; |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 381 | private final ApplicationId appId; |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 382 | |
| 383 | /** |
| 384 | * Creates a new handler registration. |
| 385 | * |
| 386 | * @param handler neighbour message handler |
| 387 | */ |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 388 | public HandlerRegistration(NeighbourMessageHandler handler, ApplicationId appId) { |
| 389 | this(handler, null, appId); |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Creates a new handler registration. |
| 394 | * |
| 395 | * @param handler neighbour message handler |
| 396 | * @param intf interface |
| 397 | */ |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 398 | public HandlerRegistration(NeighbourMessageHandler handler, Interface intf, ApplicationId appId) { |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 399 | this.intf = intf; |
| 400 | this.handler = handler; |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 401 | this.appId = appId; |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 404 | @Override |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 405 | public Interface intf() { |
| 406 | return intf; |
| 407 | } |
| 408 | |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 409 | @Override |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 410 | public NeighbourMessageHandler handler() { |
| 411 | return handler; |
| 412 | } |
| 413 | |
| 414 | @Override |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 415 | public ApplicationId appId() { |
| 416 | return appId; |
| 417 | } |
| 418 | |
| 419 | @Override |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 420 | public boolean equals(Object other) { |
| 421 | if (this == other) { |
| 422 | return true; |
| 423 | } |
| 424 | |
| 425 | if (!(other instanceof HandlerRegistration)) { |
| 426 | return false; |
| 427 | } |
| 428 | |
| 429 | HandlerRegistration that = (HandlerRegistration) other; |
| 430 | |
| 431 | return Objects.equals(intf, that.intf) && |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 432 | Objects.equals(handler, that.handler) && |
| 433 | Objects.equals(appId, that.appId); |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | @Override |
| 437 | public int hashCode() { |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 438 | return Objects.hash(intf, handler, appId); |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * Packet processor for incoming packets. |
| 444 | */ |
| 445 | private class InternalPacketProcessor implements PacketProcessor { |
| 446 | |
| 447 | @Override |
| 448 | public void process(PacketContext context) { |
| 449 | // Stop processing if the packet has been handled, since we |
| 450 | // can't do any more to it. |
| 451 | if (context.isHandled()) { |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | InboundPacket pkt = context.inPacket(); |
| 456 | Ethernet ethPkt = pkt.parsed(); |
| 457 | if (ethPkt == null) { |
| 458 | return; |
| 459 | } |
| 460 | |
| 461 | if (ethPkt.getEtherType() == TYPE_ARP) { |
| 462 | // handle ARP packets |
| 463 | handlePacket(context); |
| 464 | } else if (ethPkt.getEtherType() == TYPE_IPV6) { |
| 465 | IPv6 ipv6 = (IPv6) ethPkt.getPayload(); |
| 466 | if (ipv6.getNextHeader() == IPv6.PROTOCOL_ICMP6) { |
| 467 | ICMP6 icmp6 = (ICMP6) ipv6.getPayload(); |
| 468 | if (icmp6.getIcmpType() == NEIGHBOR_SOLICITATION || |
| 469 | icmp6.getIcmpType() == NEIGHBOR_ADVERTISEMENT) { |
| 470 | // handle ICMPv6 solicitations and advertisements (NDP) |
| 471 | handlePacket(context); |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | } |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 477 | } |