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