Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.proxyarp.impl; |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 18 | import com.google.common.collect.HashMultimap; |
| 19 | import com.google.common.collect.Lists; |
| 20 | import com.google.common.collect.Multimap; |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.apache.felix.scr.annotations.Service; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 27 | import org.onlab.packet.ARP; |
| 28 | import org.onlab.packet.Ethernet; |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 29 | import org.onlab.packet.ICMP6; |
| 30 | import org.onlab.packet.IPv6; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 31 | import org.onlab.packet.Ip4Address; |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 32 | import org.onlab.packet.Ip6Address; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 33 | import org.onlab.packet.IpAddress; |
| 34 | import org.onlab.packet.MacAddress; |
| 35 | import org.onlab.packet.VlanId; |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 36 | import org.onlab.packet.ndp.NeighborAdvertisement; |
| 37 | import org.onlab.packet.ndp.NeighborSolicitation; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.net.ConnectPoint; |
| 39 | import org.onosproject.net.Device; |
| 40 | import org.onosproject.net.Host; |
| 41 | import org.onosproject.net.HostId; |
| 42 | import org.onosproject.net.Link; |
| 43 | import org.onosproject.net.Port; |
| 44 | import org.onosproject.net.PortNumber; |
| 45 | import org.onosproject.net.device.DeviceEvent; |
| 46 | import org.onosproject.net.device.DeviceListener; |
| 47 | import org.onosproject.net.device.DeviceService; |
| 48 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 49 | import org.onosproject.net.flow.TrafficTreatment; |
| 50 | import org.onosproject.net.host.HostService; |
| 51 | import org.onosproject.net.host.InterfaceIpAddress; |
| 52 | import org.onosproject.net.host.PortAddresses; |
| 53 | import org.onosproject.net.link.LinkEvent; |
| 54 | import org.onosproject.net.link.LinkListener; |
| 55 | import org.onosproject.net.link.LinkService; |
| 56 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 57 | import org.onosproject.net.packet.InboundPacket; |
| 58 | import org.onosproject.net.packet.PacketContext; |
| 59 | import org.onosproject.net.packet.PacketService; |
| 60 | import org.onosproject.net.proxyarp.ProxyArpService; |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 61 | import org.slf4j.Logger; |
| 62 | |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 63 | import java.nio.ByteBuffer; |
| 64 | import java.util.HashSet; |
| 65 | import java.util.List; |
| 66 | import java.util.Map.Entry; |
| 67 | import java.util.Set; |
| 68 | |
| 69 | import static com.google.common.base.Preconditions.checkArgument; |
| 70 | import static com.google.common.base.Preconditions.checkNotNull; |
| 71 | import static org.slf4j.LoggerFactory.getLogger; |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 72 | |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 73 | @Component(immediate = true) |
| 74 | @Service |
| 75 | public class ProxyArpManager implements ProxyArpService { |
| 76 | |
| 77 | private final Logger log = getLogger(getClass()); |
| 78 | |
| 79 | private static final String MAC_ADDR_NULL = "Mac address cannot be null."; |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 80 | private static final String REQUEST_NULL = "ARP or NDP request cannot be null."; |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 81 | private static final String REQUEST_NOT_ARP = "Ethernet frame does not contain ARP request."; |
| 82 | private static final String NOT_ARP_REQUEST = "ARP is not a request."; |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 83 | private static final String NOT_ARP_REPLY = "ARP is not a reply."; |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 84 | |
| 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 86 | protected HostService hostService; |
| 87 | |
| 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 89 | protected PacketService packetService; |
| 90 | |
| 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 92 | protected LinkService linkService; |
| 93 | |
| 94 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 95 | protected DeviceService deviceService; |
| 96 | |
| 97 | private final Multimap<Device, PortNumber> internalPorts = |
| 98 | HashMultimap.<Device, PortNumber>create(); |
| 99 | |
| 100 | private final Multimap<Device, PortNumber> externalPorts = |
| 101 | HashMultimap.<Device, PortNumber>create(); |
| 102 | |
| 103 | /** |
| 104 | * Listens to both device service and link service to determine |
| 105 | * whether a port is internal or external. |
| 106 | */ |
| 107 | @Activate |
| 108 | public void activate() { |
| 109 | deviceService.addListener(new InternalDeviceListener()); |
| 110 | linkService.addListener(new InternalLinkListener()); |
| 111 | determinePortLocations(); |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 112 | |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 113 | log.info("Started"); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | @Deactivate |
| 118 | public void deactivate() { |
| 119 | log.info("Stopped"); |
| 120 | } |
| 121 | |
| 122 | @Override |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 123 | public boolean isKnown(IpAddress addr) { |
Yuta HIGUCHI | 5971804 | 2014-10-04 22:04:56 -0700 | [diff] [blame] | 124 | checkNotNull(addr, MAC_ADDR_NULL); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 125 | Set<Host> hosts = hostService.getHostsByIp(addr); |
| 126 | return !hosts.isEmpty(); |
| 127 | } |
| 128 | |
| 129 | @Override |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 130 | public void reply(Ethernet eth, ConnectPoint inPort) { |
Yuta HIGUCHI | 5971804 | 2014-10-04 22:04:56 -0700 | [diff] [blame] | 131 | checkNotNull(eth, REQUEST_NULL); |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 132 | |
| 133 | if (eth.getEtherType() == Ethernet.TYPE_ARP) { |
| 134 | replyArp(eth, inPort); |
| 135 | } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) { |
| 136 | replyNdp(eth, inPort); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | private void replyArp(Ethernet eth, ConnectPoint inPort) { |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 141 | ARP arp = (ARP) eth.getPayload(); |
| 142 | checkArgument(arp.getOpCode() == ARP.OP_REQUEST, NOT_ARP_REQUEST); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 143 | checkNotNull(inPort); |
| 144 | |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 145 | VlanId vlan = VlanId.vlanId(eth.getVlanID()); |
| 146 | |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 147 | // If the request came from outside the network, only reply if it was |
| 148 | // for one of our external addresses. |
| 149 | if (isOutsidePort(inPort)) { |
Pavlin Radoslavov | 5b5dc48 | 2014-11-05 14:48:08 -0800 | [diff] [blame] | 150 | Ip4Address target = |
| 151 | Ip4Address.valueOf(arp.getTargetProtocolAddress()); |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 152 | Set<PortAddresses> addressSet = |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 153 | hostService.getAddressBindingsForPort(inPort); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 154 | |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 155 | for (PortAddresses addresses : addressSet) { |
| 156 | for (InterfaceIpAddress ia : addresses.ipAddresses()) { |
| 157 | if (ia.ipAddress().equals(target)) { |
| 158 | Ethernet arpReply = |
Pavlin Radoslavov | 5b5dc48 | 2014-11-05 14:48:08 -0800 | [diff] [blame] | 159 | buildArpReply(target, addresses.mac(), eth); |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 160 | sendTo(arpReply, inPort); |
| 161 | } |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 164 | return; |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 165 | } else { |
| 166 | // If the source address matches one of our external addresses |
| 167 | // it could be a request from an internal host to an external |
Jonathan Hart | 1f793a7 | 2014-11-12 23:22:02 -0800 | [diff] [blame] | 168 | // address. Forward it over to the correct ports. |
Pavlin Radoslavov | 5b5dc48 | 2014-11-05 14:48:08 -0800 | [diff] [blame] | 169 | Ip4Address source = |
| 170 | Ip4Address.valueOf(arp.getSenderProtocolAddress()); |
Jonathan Hart | 1f793a7 | 2014-11-12 23:22:02 -0800 | [diff] [blame] | 171 | Set<PortAddresses> sourceAddresses = findPortsInSubnet(source); |
| 172 | boolean matched = false; |
| 173 | for (PortAddresses pa : sourceAddresses) { |
| 174 | for (InterfaceIpAddress ia : pa.ipAddresses()) { |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 175 | if (ia.ipAddress().equals(source) && |
| 176 | pa.vlan().equals(vlan)) { |
Jonathan Hart | 1f793a7 | 2014-11-12 23:22:02 -0800 | [diff] [blame] | 177 | matched = true; |
| 178 | sendTo(eth, pa.connectPoint()); |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | } |
Jonathan Hart | 1f793a7 | 2014-11-12 23:22:02 -0800 | [diff] [blame] | 182 | |
| 183 | if (matched) { |
| 184 | return; |
| 185 | } |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // Continue with normal proxy ARP case |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 189 | |
Pavlin Radoslavov | 5b5dc48 | 2014-11-05 14:48:08 -0800 | [diff] [blame] | 190 | Set<Host> hosts = hostService.getHostsByIp( |
| 191 | Ip4Address.valueOf(arp.getTargetProtocolAddress())); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 192 | |
| 193 | Host dst = null; |
| 194 | Host src = hostService.getHost(HostId.hostId(eth.getSourceMAC(), |
| 195 | VlanId.vlanId(eth.getVlanID()))); |
| 196 | |
| 197 | for (Host host : hosts) { |
| 198 | if (host.vlan().equals(vlan)) { |
| 199 | dst = host; |
| 200 | break; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | if (src == null || dst == null) { |
Jonathan Hart | f84591d | 2015-01-16 14:33:43 -0800 | [diff] [blame] | 205 | flood(eth, inPort); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 206 | return; |
| 207 | } |
| 208 | |
Pavlin Radoslavov | 5b5dc48 | 2014-11-05 14:48:08 -0800 | [diff] [blame] | 209 | // |
| 210 | // TODO find the correct IP address. |
| 211 | // Right now we use the first IPv4 address that is found. |
| 212 | // |
| 213 | for (IpAddress ipAddress : dst.ipAddresses()) { |
| 214 | Ip4Address ip4Address = ipAddress.getIp4Address(); |
| 215 | if (ip4Address != null) { |
| 216 | Ethernet arpReply = buildArpReply(ip4Address, dst.mac(), eth); |
| 217 | // TODO: check send status with host service. |
| 218 | sendTo(arpReply, src.location()); |
| 219 | break; |
| 220 | } |
| 221 | } |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 224 | private void replyNdp(Ethernet eth, ConnectPoint inPort) { |
| 225 | |
| 226 | IPv6 ipv6 = (IPv6) eth.getPayload(); |
| 227 | ICMP6 icmpv6 = (ICMP6) ipv6.getPayload(); |
| 228 | NeighborSolicitation nsol = (NeighborSolicitation) icmpv6.getPayload(); |
| 229 | |
| 230 | VlanId vlan = VlanId.vlanId(eth.getVlanID()); |
| 231 | |
| 232 | // If the request came from outside the network, only reply if it was |
| 233 | // for one of our external addresses. |
| 234 | if (isOutsidePort(inPort)) { |
| 235 | Ip6Address target = |
| 236 | Ip6Address.valueOf(nsol.getTargetAddress()); |
| 237 | Set<PortAddresses> addressSet = |
| 238 | hostService.getAddressBindingsForPort(inPort); |
| 239 | |
| 240 | for (PortAddresses addresses : addressSet) { |
| 241 | for (InterfaceIpAddress ia : addresses.ipAddresses()) { |
| 242 | if (ia.ipAddress().equals(target)) { |
| 243 | Ethernet ndpReply = |
| 244 | buildNdpReply(target, addresses.mac(), eth); |
| 245 | sendTo(ndpReply, inPort); |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | return; |
| 250 | } else { |
| 251 | // If the source address matches one of our external addresses |
| 252 | // it could be a request from an internal host to an external |
| 253 | // address. Forward it over to the correct ports. |
| 254 | Ip6Address source = |
| 255 | Ip6Address.valueOf(nsol.getTargetAddress()); |
| 256 | Set<PortAddresses> sourceAddresses = findPortsInSubnet(source); |
| 257 | boolean matched = false; |
| 258 | for (PortAddresses pa : sourceAddresses) { |
| 259 | for (InterfaceIpAddress ia : pa.ipAddresses()) { |
| 260 | if (ia.ipAddress().equals(source) && |
| 261 | pa.vlan().equals(vlan)) { |
| 262 | matched = true; |
| 263 | sendTo(eth, pa.connectPoint()); |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | if (matched) { |
| 269 | return; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | // Continue with normal proxy ARP case |
| 274 | |
| 275 | Set<Host> hosts = hostService.getHostsByIp( |
| 276 | Ip6Address.valueOf(nsol.getTargetAddress())); |
| 277 | |
| 278 | Host dst = null; |
| 279 | Host src = hostService.getHost(HostId.hostId(eth.getSourceMAC(), |
| 280 | VlanId.vlanId(eth.getVlanID()))); |
| 281 | |
| 282 | for (Host host : hosts) { |
| 283 | if (host.vlan().equals(vlan)) { |
| 284 | dst = host; |
| 285 | break; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | if (src == null || dst == null) { |
| 290 | flood(eth, inPort); |
| 291 | return; |
| 292 | } |
| 293 | |
| 294 | // |
| 295 | // TODO find the correct IP address. |
| 296 | // Right now we use the first IPv4 address that is found. |
| 297 | // |
| 298 | for (IpAddress ipAddress : dst.ipAddresses()) { |
| 299 | Ip6Address ip6Address = ipAddress.getIp6Address(); |
| 300 | if (ip6Address != null) { |
| 301 | Ethernet arpReply = buildNdpReply(ip6Address, dst.mac(), eth); |
| 302 | // TODO: check send status with host service. |
| 303 | sendTo(arpReply, src.location()); |
| 304 | break; |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 310 | /** |
| 311 | * Outputs the given packet out the given port. |
| 312 | * |
| 313 | * @param packet the packet to send |
| 314 | * @param outPort the port to send it out |
| 315 | */ |
| 316 | private void sendTo(Ethernet packet, ConnectPoint outPort) { |
| 317 | if (internalPorts.containsEntry( |
| 318 | deviceService.getDevice(outPort.deviceId()), outPort.port())) { |
| 319 | // Sanity check to make sure we don't send the packet out an |
| 320 | // internal port and create a loop (could happen due to |
| 321 | // misconfiguration). |
| 322 | return; |
| 323 | } |
| 324 | |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 325 | TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder(); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 326 | builder.setOutput(outPort.port()); |
| 327 | packetService.emit(new DefaultOutboundPacket(outPort.deviceId(), |
| 328 | builder.build(), ByteBuffer.wrap(packet.serialize()))); |
| 329 | } |
| 330 | |
| 331 | /** |
Jonathan Hart | 1f793a7 | 2014-11-12 23:22:02 -0800 | [diff] [blame] | 332 | * Finds ports with an address in the subnet of the target address. |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 333 | * |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 334 | * @param target the target address to find a matching port for |
Jonathan Hart | 1f793a7 | 2014-11-12 23:22:02 -0800 | [diff] [blame] | 335 | * @return a set of PortAddresses describing ports in the subnet |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 336 | */ |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 337 | private Set<PortAddresses> findPortsInSubnet(IpAddress target) { |
Jonathan Hart | 1f793a7 | 2014-11-12 23:22:02 -0800 | [diff] [blame] | 338 | Set<PortAddresses> result = new HashSet<PortAddresses>(); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 339 | for (PortAddresses addresses : hostService.getAddressBindings()) { |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 340 | for (InterfaceIpAddress ia : addresses.ipAddresses()) { |
| 341 | if (ia.subnetAddress().contains(target)) { |
Jonathan Hart | 1f793a7 | 2014-11-12 23:22:02 -0800 | [diff] [blame] | 342 | result.add(addresses); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | } |
Jonathan Hart | 1f793a7 | 2014-11-12 23:22:02 -0800 | [diff] [blame] | 346 | return result; |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Returns whether the given port is an outside-facing port with an IP |
| 351 | * address configured. |
| 352 | * |
| 353 | * @param port the port to check |
| 354 | * @return true if the port is an outside-facing port, otherwise false |
| 355 | */ |
| 356 | private boolean isOutsidePort(ConnectPoint port) { |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 357 | // |
| 358 | // TODO: Is this sufficient to identify outside-facing ports: just |
| 359 | // having IP addresses on a port? |
| 360 | // |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 361 | return !hostService.getAddressBindingsForPort(port).isEmpty(); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | @Override |
Jonathan Hart | f84591d | 2015-01-16 14:33:43 -0800 | [diff] [blame] | 365 | public void forward(Ethernet eth, ConnectPoint inPort) { |
Yuta HIGUCHI | 5971804 | 2014-10-04 22:04:56 -0700 | [diff] [blame] | 366 | checkNotNull(eth, REQUEST_NULL); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 367 | |
| 368 | Host h = hostService.getHost(HostId.hostId(eth.getDestinationMAC(), |
| 369 | VlanId.vlanId(eth.getVlanID()))); |
| 370 | |
| 371 | if (h == null) { |
Jonathan Hart | f84591d | 2015-01-16 14:33:43 -0800 | [diff] [blame] | 372 | flood(eth, inPort); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 373 | } else { |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 374 | TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder(); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 375 | builder.setOutput(h.location().port()); |
| 376 | packetService.emit(new DefaultOutboundPacket(h.location().deviceId(), |
| 377 | builder.build(), ByteBuffer.wrap(eth.serialize()))); |
| 378 | } |
| 379 | |
| 380 | } |
| 381 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 382 | @Override |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 383 | public boolean handlePacket(PacketContext context) { |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 384 | InboundPacket pkt = context.inPacket(); |
| 385 | Ethernet ethPkt = pkt.parsed(); |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 386 | |
| 387 | if (ethPkt == null) { |
| 388 | return false; |
| 389 | } |
| 390 | if (ethPkt.getEtherType() == Ethernet.TYPE_ARP) { |
| 391 | return handleArp(context, ethPkt); |
| 392 | } else if (ethPkt.getEtherType() == Ethernet.TYPE_IPV6) { |
| 393 | return handleNdp(context, ethPkt); |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 394 | } |
| 395 | return false; |
| 396 | } |
| 397 | |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 398 | private boolean handleArp(PacketContext context, Ethernet ethPkt) { |
| 399 | ARP arp = (ARP) ethPkt.getPayload(); |
| 400 | |
| 401 | if (arp.getOpCode() == ARP.OP_REPLY) { |
| 402 | forward(ethPkt, context.inPacket().receivedFrom()); |
| 403 | } else if (arp.getOpCode() == ARP.OP_REQUEST) { |
| 404 | reply(ethPkt, context.inPacket().receivedFrom()); |
| 405 | } else { |
| 406 | return false; |
| 407 | } |
| 408 | context.block(); |
| 409 | return true; |
| 410 | } |
| 411 | |
| 412 | private boolean handleNdp(PacketContext context, Ethernet ethPkt) { |
| 413 | IPv6 ipv6 = (IPv6) ethPkt.getPayload(); |
| 414 | |
| 415 | if (ipv6.getNextHeader() != IPv6.PROTOCOL_ICMP6) { |
| 416 | return false; |
| 417 | } |
| 418 | ICMP6 icmpv6 = (ICMP6) ipv6.getPayload(); |
| 419 | if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_ADVERTISEMENT) { |
| 420 | forward(ethPkt, context.inPacket().receivedFrom()); |
| 421 | } else if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_SOLICITATION) { |
| 422 | reply(ethPkt, context.inPacket().receivedFrom()); |
| 423 | } else { |
| 424 | return false; |
| 425 | } |
| 426 | context.block(); |
| 427 | return true; |
| 428 | } |
| 429 | |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 430 | /** |
| 431 | * Flood the arp request at all edges in the network. |
| 432 | * @param request the arp request. |
| 433 | */ |
Jonathan Hart | f84591d | 2015-01-16 14:33:43 -0800 | [diff] [blame] | 434 | private void flood(Ethernet request, ConnectPoint inPort) { |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 435 | TrafficTreatment.Builder builder = null; |
| 436 | ByteBuffer buf = ByteBuffer.wrap(request.serialize()); |
| 437 | |
| 438 | synchronized (externalPorts) { |
| 439 | for (Entry<Device, PortNumber> entry : externalPorts.entries()) { |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 440 | ConnectPoint cp = new ConnectPoint(entry.getKey().id(), entry.getValue()); |
Jonathan Hart | f84591d | 2015-01-16 14:33:43 -0800 | [diff] [blame] | 441 | if (isOutsidePort(cp) || cp.equals(inPort)) { |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 442 | continue; |
| 443 | } |
| 444 | |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 445 | builder = DefaultTrafficTreatment.builder(); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 446 | builder.setOutput(entry.getValue()); |
| 447 | packetService.emit(new DefaultOutboundPacket(entry.getKey().id(), |
| 448 | builder.build(), buf)); |
| 449 | } |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Determines the location of all known ports in the system. |
| 455 | */ |
| 456 | private void determinePortLocations() { |
| 457 | Iterable<Device> devices = deviceService.getDevices(); |
| 458 | Iterable<Link> links = null; |
| 459 | List<PortNumber> ports = null; |
| 460 | for (Device d : devices) { |
| 461 | ports = buildPortNumberList(deviceService.getPorts(d.id())); |
| 462 | links = linkService.getLinks(); |
| 463 | for (Link l : links) { |
| 464 | // for each link, mark the concerned ports as internal |
| 465 | // and the remaining ports are therefore external. |
Yuta HIGUCHI | 3541bf2 | 2014-10-04 22:06:19 -0700 | [diff] [blame] | 466 | if (l.src().deviceId().equals(d.id()) |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 467 | && ports.contains(l.src().port())) { |
| 468 | ports.remove(l.src().port()); |
| 469 | internalPorts.put(d, l.src().port()); |
| 470 | } |
Yuta HIGUCHI | 3541bf2 | 2014-10-04 22:06:19 -0700 | [diff] [blame] | 471 | if (l.dst().deviceId().equals(d.id()) |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 472 | && ports.contains(l.dst().port())) { |
| 473 | ports.remove(l.dst().port()); |
| 474 | internalPorts.put(d, l.dst().port()); |
| 475 | } |
| 476 | } |
| 477 | synchronized (externalPorts) { |
| 478 | externalPorts.putAll(d, ports); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | } |
| 483 | |
| 484 | private List<PortNumber> buildPortNumberList(List<Port> ports) { |
| 485 | List<PortNumber> portNumbers = Lists.newLinkedList(); |
| 486 | for (Port p : ports) { |
| 487 | portNumbers.add(p.number()); |
| 488 | } |
| 489 | return portNumbers; |
| 490 | } |
| 491 | |
| 492 | /** |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 493 | * Builds an ARP reply based on a request. |
| 494 | * |
| 495 | * @param srcIp the IP address to use as the reply source |
| 496 | * @param srcMac the MAC address to use as the reply source |
| 497 | * @param request the ARP request we got |
| 498 | * @return an Ethernet frame containing the ARP reply |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 499 | */ |
Pavlin Radoslavov | 5b5dc48 | 2014-11-05 14:48:08 -0800 | [diff] [blame] | 500 | private Ethernet buildArpReply(Ip4Address srcIp, MacAddress srcMac, |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 501 | Ethernet request) { |
| 502 | |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 503 | Ethernet eth = new Ethernet(); |
Yuta HIGUCHI | 3e848a8 | 2014-11-02 20:19:42 -0800 | [diff] [blame] | 504 | eth.setDestinationMACAddress(request.getSourceMAC()); |
| 505 | eth.setSourceMACAddress(srcMac); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 506 | eth.setEtherType(Ethernet.TYPE_ARP); |
| 507 | eth.setVlanID(request.getVlanID()); |
| 508 | |
| 509 | ARP arp = new ARP(); |
| 510 | arp.setOpCode(ARP.OP_REPLY); |
| 511 | arp.setProtocolType(ARP.PROTO_TYPE_IP); |
| 512 | arp.setHardwareType(ARP.HW_TYPE_ETHERNET); |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 513 | |
Pavlin Radoslavov | 5b5dc48 | 2014-11-05 14:48:08 -0800 | [diff] [blame] | 514 | arp.setProtocolAddressLength((byte) Ip4Address.BYTE_LENGTH); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 515 | arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH); |
Yuta HIGUCHI | 3e848a8 | 2014-11-02 20:19:42 -0800 | [diff] [blame] | 516 | arp.setSenderHardwareAddress(srcMac.toBytes()); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 517 | arp.setTargetHardwareAddress(request.getSourceMACAddress()); |
| 518 | |
| 519 | arp.setTargetProtocolAddress(((ARP) request.getPayload()) |
| 520 | .getSenderProtocolAddress()); |
Jonathan Hart | bcae7bd | 2014-10-16 10:24:41 -0700 | [diff] [blame] | 521 | arp.setSenderProtocolAddress(srcIp.toInt()); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 522 | eth.setPayload(arp); |
| 523 | return eth; |
| 524 | } |
| 525 | |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 526 | /** |
| 527 | * Builds an Neighbor Discovery reply based on a request. |
| 528 | * |
| 529 | * @param srcIp the IP address to use as the reply source |
| 530 | * @param srcMac the MAC address to use as the reply source |
| 531 | * @param request the Neighbor Solicitation request we got |
| 532 | * @return an Ethernet frame containing the Neighbor Advertisement reply |
| 533 | */ |
| 534 | private Ethernet buildNdpReply(Ip6Address srcIp, MacAddress srcMac, |
| 535 | Ethernet request) { |
| 536 | |
| 537 | Ethernet eth = new Ethernet(); |
| 538 | eth.setDestinationMACAddress(request.getSourceMAC()); |
| 539 | eth.setSourceMACAddress(srcMac); |
| 540 | eth.setEtherType(Ethernet.TYPE_IPV6); |
| 541 | eth.setVlanID(request.getVlanID()); |
| 542 | |
| 543 | IPv6 requestIp = (IPv6) request.getPayload(); |
| 544 | IPv6 ipv6 = new IPv6(); |
| 545 | ipv6.setSourceAddress(srcIp.toOctets()); |
| 546 | ipv6.setDestinationAddress(requestIp.getSourceAddress()); |
| 547 | ipv6.setHopLimit((byte) 255); |
| 548 | eth.setPayload(ipv6); |
| 549 | |
| 550 | ICMP6 icmp6 = new ICMP6(); |
| 551 | icmp6.setIcmpType(ICMP6.NEIGHBOR_ADVERTISEMENT); |
| 552 | icmp6.setIcmpCode((byte) 0); |
| 553 | ipv6.setPayload(icmp6); |
| 554 | |
| 555 | NeighborAdvertisement nadv = new NeighborAdvertisement(); |
| 556 | nadv.setTargetAddress(srcMac.toBytes()); |
| 557 | nadv.setSolicitedFlag((byte) 1); |
| 558 | nadv.setOverrideFlag((byte) 1); |
| 559 | icmp6.setPayload(nadv); |
| 560 | |
| 561 | return eth; |
| 562 | } |
| 563 | |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 564 | public class InternalLinkListener implements LinkListener { |
| 565 | |
| 566 | @Override |
| 567 | public void event(LinkEvent event) { |
| 568 | Link link = event.subject(); |
| 569 | Device src = deviceService.getDevice(link.src().deviceId()); |
| 570 | Device dst = deviceService.getDevice(link.dst().deviceId()); |
| 571 | switch (event.type()) { |
| 572 | case LINK_ADDED: |
| 573 | synchronized (externalPorts) { |
| 574 | externalPorts.remove(src, link.src().port()); |
| 575 | externalPorts.remove(dst, link.dst().port()); |
| 576 | internalPorts.put(src, link.src().port()); |
| 577 | internalPorts.put(dst, link.dst().port()); |
| 578 | } |
| 579 | |
| 580 | break; |
| 581 | case LINK_REMOVED: |
| 582 | synchronized (externalPorts) { |
| 583 | externalPorts.put(src, link.src().port()); |
| 584 | externalPorts.put(dst, link.dst().port()); |
| 585 | internalPorts.remove(src, link.src().port()); |
| 586 | internalPorts.remove(dst, link.dst().port()); |
| 587 | } |
| 588 | |
| 589 | break; |
| 590 | case LINK_UPDATED: |
| 591 | // don't care about links being updated. |
| 592 | break; |
| 593 | default: |
| 594 | break; |
| 595 | } |
| 596 | |
| 597 | } |
| 598 | |
| 599 | } |
| 600 | |
| 601 | public class InternalDeviceListener implements DeviceListener { |
| 602 | |
| 603 | @Override |
| 604 | public void event(DeviceEvent event) { |
| 605 | Device device = event.subject(); |
| 606 | switch (event.type()) { |
| 607 | case DEVICE_ADDED: |
| 608 | case DEVICE_AVAILABILITY_CHANGED: |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 609 | case DEVICE_SUSPENDED: |
| 610 | case DEVICE_UPDATED: |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 611 | // nothing to do in these cases; handled when links get reported |
| 612 | break; |
| 613 | case DEVICE_REMOVED: |
| 614 | synchronized (externalPorts) { |
| 615 | externalPorts.removeAll(device); |
| 616 | internalPorts.removeAll(device); |
| 617 | } |
| 618 | break; |
| 619 | case PORT_ADDED: |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 620 | case PORT_UPDATED: |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 621 | synchronized (externalPorts) { |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 622 | if (event.port().isEnabled()) { |
| 623 | externalPorts.put(device, event.port().number()); |
| 624 | internalPorts.remove(device, event.port().number()); |
| 625 | } |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 626 | } |
| 627 | break; |
| 628 | case PORT_REMOVED: |
| 629 | synchronized (externalPorts) { |
| 630 | externalPorts.remove(device, event.port().number()); |
| 631 | internalPorts.remove(device, event.port().number()); |
| 632 | } |
| 633 | break; |
| 634 | default: |
| 635 | break; |
| 636 | |
| 637 | } |
| 638 | |
| 639 | } |
| 640 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 641 | } |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 642 | |
| 643 | } |