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