Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -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 | */ |
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 | |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.apache.felix.scr.annotations.Service; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 24 | import org.onlab.packet.ARP; |
| 25 | import org.onlab.packet.Ethernet; |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 26 | import org.onlab.packet.ICMP6; |
| 27 | import org.onlab.packet.IPv6; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 28 | import org.onlab.packet.Ip4Address; |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 29 | import org.onlab.packet.Ip6Address; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 30 | import org.onlab.packet.IpAddress; |
| 31 | import org.onlab.packet.MacAddress; |
| 32 | import org.onlab.packet.VlanId; |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 33 | import org.onlab.packet.ndp.NeighborAdvertisement; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 34 | import org.onlab.packet.ndp.NeighborDiscoveryOptions; |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 35 | import org.onlab.packet.ndp.NeighborSolicitation; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 36 | import org.onosproject.core.Permission; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 37 | import org.onosproject.net.ConnectPoint; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.net.Host; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 39 | import org.onosproject.net.device.DeviceService; |
Aaron Kruglikov | d812383 | 2015-07-06 14:20:25 -0700 | [diff] [blame] | 40 | import org.onosproject.net.edge.EdgePortService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 41 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 42 | import org.onosproject.net.flow.TrafficTreatment; |
| 43 | import org.onosproject.net.host.HostService; |
| 44 | import org.onosproject.net.host.InterfaceIpAddress; |
| 45 | import org.onosproject.net.host.PortAddresses; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 46 | import org.onosproject.net.link.LinkService; |
| 47 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 48 | import org.onosproject.net.packet.InboundPacket; |
| 49 | import org.onosproject.net.packet.PacketContext; |
| 50 | import org.onosproject.net.packet.PacketService; |
| 51 | import org.onosproject.net.proxyarp.ProxyArpService; |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame^] | 52 | import org.onosproject.net.proxyarp.ProxyArpStore; |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 53 | import org.slf4j.Logger; |
| 54 | |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 55 | import java.nio.ByteBuffer; |
| 56 | import java.util.HashSet; |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 57 | import java.util.Set; |
Aaron Kruglikov | d812383 | 2015-07-06 14:20:25 -0700 | [diff] [blame] | 58 | import java.util.stream.Collectors; |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 59 | |
| 60 | import static com.google.common.base.Preconditions.checkArgument; |
| 61 | import static com.google.common.base.Preconditions.checkNotNull; |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame^] | 62 | import static org.onlab.packet.VlanId.vlanId; |
| 63 | import static org.onosproject.net.HostId.hostId; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 64 | import static org.onosproject.security.AppGuard.checkPermission; |
Aaron Kruglikov | d812383 | 2015-07-06 14:20:25 -0700 | [diff] [blame] | 65 | import static org.slf4j.LoggerFactory.getLogger; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 66 | |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 67 | |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 68 | @Component(immediate = true) |
| 69 | @Service |
| 70 | public class ProxyArpManager implements ProxyArpService { |
| 71 | |
| 72 | private final Logger log = getLogger(getClass()); |
| 73 | |
| 74 | private static final String MAC_ADDR_NULL = "Mac address cannot be null."; |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 75 | private static final String REQUEST_NULL = "ARP or NDP request cannot be null."; |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 76 | private static final String REQUEST_NOT_ARP = "Ethernet frame does not contain ARP request."; |
| 77 | private static final String NOT_ARP_REQUEST = "ARP is not a request."; |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 78 | private static final String NOT_ARP_REPLY = "ARP is not a reply."; |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 79 | |
| 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Aaron Kruglikov | d812383 | 2015-07-06 14:20:25 -0700 | [diff] [blame] | 81 | protected EdgePortService edgeService; |
| 82 | |
| 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 84 | protected HostService hostService; |
| 85 | |
| 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 87 | protected PacketService packetService; |
| 88 | |
| 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 90 | protected LinkService linkService; |
| 91 | |
| 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 93 | protected DeviceService deviceService; |
| 94 | |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame^] | 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 96 | protected ProxyArpStore store; |
| 97 | |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 98 | /** |
| 99 | * Listens to both device service and link service to determine |
| 100 | * whether a port is internal or external. |
| 101 | */ |
| 102 | @Activate |
| 103 | public void activate() { |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame^] | 104 | store.setDelegate(this::sendTo); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 105 | log.info("Started"); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | @Deactivate |
| 110 | public void deactivate() { |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame^] | 111 | store.setDelegate(null); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 112 | log.info("Stopped"); |
| 113 | } |
| 114 | |
| 115 | @Override |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 116 | public boolean isKnown(IpAddress addr) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 117 | checkPermission(Permission.PACKET_READ); |
Yuta HIGUCHI | 5971804 | 2014-10-04 22:04:56 -0700 | [diff] [blame] | 118 | checkNotNull(addr, MAC_ADDR_NULL); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 119 | Set<Host> hosts = hostService.getHostsByIp(addr); |
| 120 | return !hosts.isEmpty(); |
| 121 | } |
| 122 | |
| 123 | @Override |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 124 | public void reply(Ethernet eth, ConnectPoint inPort) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 125 | checkPermission(Permission.PACKET_WRITE); |
Yuta HIGUCHI | 5971804 | 2014-10-04 22:04:56 -0700 | [diff] [blame] | 126 | checkNotNull(eth, REQUEST_NULL); |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 127 | |
| 128 | if (eth.getEtherType() == Ethernet.TYPE_ARP) { |
| 129 | replyArp(eth, inPort); |
| 130 | } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) { |
| 131 | replyNdp(eth, inPort); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | private void replyArp(Ethernet eth, ConnectPoint inPort) { |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 136 | ARP arp = (ARP) eth.getPayload(); |
| 137 | checkArgument(arp.getOpCode() == ARP.OP_REQUEST, NOT_ARP_REQUEST); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 138 | checkNotNull(inPort); |
Dusan Pajin | a22b970 | 2015-02-12 16:25:23 +0100 | [diff] [blame] | 139 | Ip4Address targetAddress = Ip4Address.valueOf(arp.getTargetProtocolAddress()); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 140 | |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame^] | 141 | VlanId vlan = vlanId(eth.getVlanID()); |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 142 | |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 143 | if (isOutsidePort(inPort)) { |
Jonathan Hart | 7d1496b | 2015-03-10 22:00:48 -0700 | [diff] [blame] | 144 | // If the request came from outside the network, only reply if it was |
| 145 | // for one of our external addresses. |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 146 | Set<PortAddresses> addressSet = |
Jonathan Hart | 7d1496b | 2015-03-10 22:00:48 -0700 | [diff] [blame] | 147 | hostService.getAddressBindingsForPort(inPort); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 148 | |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 149 | for (PortAddresses addresses : addressSet) { |
| 150 | for (InterfaceIpAddress ia : addresses.ipAddresses()) { |
Dusan Pajin | a22b970 | 2015-02-12 16:25:23 +0100 | [diff] [blame] | 151 | if (ia.ipAddress().equals(targetAddress)) { |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 152 | Ethernet arpReply = |
Pingping Lin | c9e16bf | 2015-04-10 14:42:41 -0700 | [diff] [blame] | 153 | ARP.buildArpReply(targetAddress, addresses.mac(), eth); |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 154 | sendTo(arpReply, inPort); |
| 155 | } |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 158 | return; |
| 159 | } |
| 160 | |
Jonathan Hart | 7d1496b | 2015-03-10 22:00:48 -0700 | [diff] [blame] | 161 | // See if we have the target host in the host store |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 162 | |
Dusan Pajin | a22b970 | 2015-02-12 16:25:23 +0100 | [diff] [blame] | 163 | Set<Host> hosts = hostService.getHostsByIp(targetAddress); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 164 | |
| 165 | Host dst = null; |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame^] | 166 | Host src = hostService.getHost(hostId(eth.getSourceMAC(), |
| 167 | vlanId(eth.getVlanID()))); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 168 | |
| 169 | for (Host host : hosts) { |
| 170 | if (host.vlan().equals(vlan)) { |
| 171 | dst = host; |
| 172 | break; |
| 173 | } |
| 174 | } |
| 175 | |
Jonathan Hart | 7d1496b | 2015-03-10 22:00:48 -0700 | [diff] [blame] | 176 | if (src != null && dst != null) { |
| 177 | // We know the target host so we can respond |
Pingping Lin | c9e16bf | 2015-04-10 14:42:41 -0700 | [diff] [blame] | 178 | Ethernet arpReply = ARP.buildArpReply(targetAddress, dst.mac(), eth); |
Jonathan Hart | 7d1496b | 2015-03-10 22:00:48 -0700 | [diff] [blame] | 179 | sendTo(arpReply, inPort); |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | // If the source address matches one of our external addresses |
| 184 | // it could be a request from an internal host to an external |
| 185 | // address. Forward it over to the correct port. |
| 186 | Ip4Address source = |
| 187 | Ip4Address.valueOf(arp.getSenderProtocolAddress()); |
| 188 | Set<PortAddresses> sourceAddresses = findPortsInSubnet(source); |
| 189 | boolean matched = false; |
| 190 | for (PortAddresses pa : sourceAddresses) { |
| 191 | for (InterfaceIpAddress ia : pa.ipAddresses()) { |
| 192 | if (ia.ipAddress().equals(source) && |
| 193 | pa.vlan().equals(vlan)) { |
| 194 | matched = true; |
| 195 | sendTo(eth, pa.connectPoint()); |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | if (matched) { |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 202 | return; |
| 203 | } |
| 204 | |
Pavlin Radoslavov | 5b5dc48 | 2014-11-05 14:48:08 -0800 | [diff] [blame] | 205 | // |
Jonathan Hart | 7d1496b | 2015-03-10 22:00:48 -0700 | [diff] [blame] | 206 | // The request couldn't be resolved. |
| 207 | // Flood the request on all ports except the incoming port. |
Pavlin Radoslavov | 5b5dc48 | 2014-11-05 14:48:08 -0800 | [diff] [blame] | 208 | // |
Jonathan Hart | 7d1496b | 2015-03-10 22:00:48 -0700 | [diff] [blame] | 209 | flood(eth, inPort); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 212 | private void replyNdp(Ethernet eth, ConnectPoint inPort) { |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 213 | IPv6 ipv6 = (IPv6) eth.getPayload(); |
| 214 | ICMP6 icmpv6 = (ICMP6) ipv6.getPayload(); |
| 215 | NeighborSolicitation nsol = (NeighborSolicitation) icmpv6.getPayload(); |
Dusan Pajin | a22b970 | 2015-02-12 16:25:23 +0100 | [diff] [blame] | 216 | Ip6Address targetAddress = Ip6Address.valueOf(nsol.getTargetAddress()); |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 217 | |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame^] | 218 | VlanId vlan = vlanId(eth.getVlanID()); |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 219 | |
| 220 | // If the request came from outside the network, only reply if it was |
| 221 | // for one of our external addresses. |
| 222 | if (isOutsidePort(inPort)) { |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 223 | Set<PortAddresses> addressSet = |
| 224 | hostService.getAddressBindingsForPort(inPort); |
| 225 | |
| 226 | for (PortAddresses addresses : addressSet) { |
| 227 | for (InterfaceIpAddress ia : addresses.ipAddresses()) { |
Dusan Pajin | a22b970 | 2015-02-12 16:25:23 +0100 | [diff] [blame] | 228 | if (ia.ipAddress().equals(targetAddress)) { |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 229 | Ethernet ndpReply = |
Aaron Kruglikov | d812383 | 2015-07-06 14:20:25 -0700 | [diff] [blame] | 230 | buildNdpReply(targetAddress, addresses.mac(), eth); |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 231 | sendTo(ndpReply, inPort); |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | return; |
| 236 | } else { |
| 237 | // If the source address matches one of our external addresses |
| 238 | // it could be a request from an internal host to an external |
| 239 | // address. Forward it over to the correct ports. |
| 240 | Ip6Address source = |
Pavlin Radoslavov | cef5206 | 2015-02-12 16:57:17 -0800 | [diff] [blame] | 241 | Ip6Address.valueOf(ipv6.getSourceAddress()); |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 242 | Set<PortAddresses> sourceAddresses = findPortsInSubnet(source); |
| 243 | boolean matched = false; |
| 244 | for (PortAddresses pa : sourceAddresses) { |
| 245 | for (InterfaceIpAddress ia : pa.ipAddresses()) { |
| 246 | if (ia.ipAddress().equals(source) && |
| 247 | pa.vlan().equals(vlan)) { |
| 248 | matched = true; |
| 249 | sendTo(eth, pa.connectPoint()); |
Pavlin Radoslavov | cef5206 | 2015-02-12 16:57:17 -0800 | [diff] [blame] | 250 | break; |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | if (matched) { |
| 256 | return; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | // Continue with normal proxy ARP case |
| 261 | |
Dusan Pajin | a22b970 | 2015-02-12 16:25:23 +0100 | [diff] [blame] | 262 | Set<Host> hosts = hostService.getHostsByIp(targetAddress); |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 263 | |
| 264 | Host dst = null; |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame^] | 265 | Host src = hostService.getHost(hostId(eth.getSourceMAC(), |
| 266 | vlanId(eth.getVlanID()))); |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 267 | |
| 268 | for (Host host : hosts) { |
| 269 | if (host.vlan().equals(vlan)) { |
| 270 | dst = host; |
| 271 | break; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | if (src == null || dst == null) { |
Dusan Pajin | a22b970 | 2015-02-12 16:25:23 +0100 | [diff] [blame] | 276 | // |
| 277 | // The request couldn't be resolved. |
| 278 | // Flood the request on all ports except the incoming ports. |
| 279 | // |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 280 | flood(eth, inPort); |
| 281 | return; |
| 282 | } |
| 283 | |
| 284 | // |
Dusan Pajin | a22b970 | 2015-02-12 16:25:23 +0100 | [diff] [blame] | 285 | // Reply on the port the request was received on |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 286 | // |
Dusan Pajin | a22b970 | 2015-02-12 16:25:23 +0100 | [diff] [blame] | 287 | Ethernet ndpReply = buildNdpReply(targetAddress, dst.mac(), eth); |
| 288 | sendTo(ndpReply, inPort); |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 289 | } |
Aaron Kruglikov | d812383 | 2015-07-06 14:20:25 -0700 | [diff] [blame] | 290 | //TODO checkpoint |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 291 | |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 292 | /** |
| 293 | * Outputs the given packet out the given port. |
| 294 | * |
Aaron Kruglikov | d812383 | 2015-07-06 14:20:25 -0700 | [diff] [blame] | 295 | * @param packet the packet to send |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 296 | * @param outPort the port to send it out |
| 297 | */ |
| 298 | private void sendTo(Ethernet packet, ConnectPoint outPort) { |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame^] | 299 | sendTo(outPort, ByteBuffer.wrap(packet.serialize())); |
| 300 | } |
| 301 | |
| 302 | private void sendTo(ConnectPoint outPort, ByteBuffer packet) { |
Aaron Kruglikov | d812383 | 2015-07-06 14:20:25 -0700 | [diff] [blame] | 303 | if (!edgeService.isEdgePoint(outPort)) { |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 304 | // Sanity check to make sure we don't send the packet out an |
| 305 | // internal port and create a loop (could happen due to |
| 306 | // misconfiguration). |
| 307 | return; |
| 308 | } |
| 309 | |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 310 | TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder(); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 311 | builder.setOutput(outPort.port()); |
| 312 | packetService.emit(new DefaultOutboundPacket(outPort.deviceId(), |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame^] | 313 | builder.build(), packet)); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /** |
Jonathan Hart | 1f793a7 | 2014-11-12 23:22:02 -0800 | [diff] [blame] | 317 | * Finds ports with an address in the subnet of the target address. |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 318 | * |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 319 | * @param target the target address to find a matching port for |
Jonathan Hart | 1f793a7 | 2014-11-12 23:22:02 -0800 | [diff] [blame] | 320 | * @return a set of PortAddresses describing ports in the subnet |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 321 | */ |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 322 | private Set<PortAddresses> findPortsInSubnet(IpAddress target) { |
Aaron Kruglikov | d812383 | 2015-07-06 14:20:25 -0700 | [diff] [blame] | 323 | Set<PortAddresses> result = new HashSet<>(); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 324 | for (PortAddresses addresses : hostService.getAddressBindings()) { |
Aaron Kruglikov | d812383 | 2015-07-06 14:20:25 -0700 | [diff] [blame] | 325 | result.addAll(addresses.ipAddresses().stream().filter(ia -> ia.subnetAddress().contains(target)). |
| 326 | map(ia -> addresses).collect(Collectors.toList())); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 327 | } |
Jonathan Hart | 1f793a7 | 2014-11-12 23:22:02 -0800 | [diff] [blame] | 328 | return result; |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Returns whether the given port is an outside-facing port with an IP |
| 333 | * address configured. |
| 334 | * |
| 335 | * @param port the port to check |
| 336 | * @return true if the port is an outside-facing port, otherwise false |
| 337 | */ |
| 338 | private boolean isOutsidePort(ConnectPoint port) { |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame^] | 339 | // TODO: Is this sufficient to identify outside-facing ports: just having IP addresses on a port? |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 340 | return !hostService.getAddressBindingsForPort(port).isEmpty(); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | @Override |
Jonathan Hart | f84591d | 2015-01-16 14:33:43 -0800 | [diff] [blame] | 344 | public void forward(Ethernet eth, ConnectPoint inPort) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 345 | checkPermission(Permission.PACKET_WRITE); |
Yuta HIGUCHI | 5971804 | 2014-10-04 22:04:56 -0700 | [diff] [blame] | 346 | checkNotNull(eth, REQUEST_NULL); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 347 | |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame^] | 348 | Host h = hostService.getHost(hostId(eth.getDestinationMAC(), |
| 349 | vlanId(eth.getVlanID()))); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 350 | |
| 351 | if (h == null) { |
Jonathan Hart | f84591d | 2015-01-16 14:33:43 -0800 | [diff] [blame] | 352 | flood(eth, inPort); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 353 | } else { |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame^] | 354 | Host subject = hostService.getHost(hostId(eth.getSourceMAC(), |
| 355 | vlanId(eth.getVlanID()))); |
| 356 | store.forward(h.location(), subject, ByteBuffer.wrap(eth.serialize())); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 357 | } |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 358 | } |
| 359 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 360 | @Override |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 361 | public boolean handlePacket(PacketContext context) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 362 | checkPermission(Permission.PACKET_WRITE); |
| 363 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 364 | InboundPacket pkt = context.inPacket(); |
| 365 | Ethernet ethPkt = pkt.parsed(); |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 366 | |
| 367 | if (ethPkt == null) { |
| 368 | return false; |
| 369 | } |
| 370 | if (ethPkt.getEtherType() == Ethernet.TYPE_ARP) { |
| 371 | return handleArp(context, ethPkt); |
| 372 | } else if (ethPkt.getEtherType() == Ethernet.TYPE_IPV6) { |
| 373 | return handleNdp(context, ethPkt); |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 374 | } |
| 375 | return false; |
| 376 | } |
| 377 | |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 378 | private boolean handleArp(PacketContext context, Ethernet ethPkt) { |
| 379 | ARP arp = (ARP) ethPkt.getPayload(); |
| 380 | |
| 381 | if (arp.getOpCode() == ARP.OP_REPLY) { |
| 382 | forward(ethPkt, context.inPacket().receivedFrom()); |
| 383 | } else if (arp.getOpCode() == ARP.OP_REQUEST) { |
| 384 | reply(ethPkt, context.inPacket().receivedFrom()); |
| 385 | } else { |
| 386 | return false; |
| 387 | } |
| 388 | context.block(); |
| 389 | return true; |
| 390 | } |
| 391 | |
| 392 | private boolean handleNdp(PacketContext context, Ethernet ethPkt) { |
| 393 | IPv6 ipv6 = (IPv6) ethPkt.getPayload(); |
| 394 | |
| 395 | if (ipv6.getNextHeader() != IPv6.PROTOCOL_ICMP6) { |
| 396 | return false; |
| 397 | } |
| 398 | ICMP6 icmpv6 = (ICMP6) ipv6.getPayload(); |
| 399 | if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_ADVERTISEMENT) { |
| 400 | forward(ethPkt, context.inPacket().receivedFrom()); |
| 401 | } else if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_SOLICITATION) { |
| 402 | reply(ethPkt, context.inPacket().receivedFrom()); |
| 403 | } else { |
| 404 | return false; |
| 405 | } |
| 406 | context.block(); |
| 407 | return true; |
| 408 | } |
| 409 | |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 410 | /** |
| 411 | * Flood the arp request at all edges in the network. |
Dusan Pajin | a22b970 | 2015-02-12 16:25:23 +0100 | [diff] [blame] | 412 | * |
| 413 | * @param request the arp request |
Aaron Kruglikov | d812383 | 2015-07-06 14:20:25 -0700 | [diff] [blame] | 414 | * @param inPort the connect point the arp request was received on |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 415 | */ |
Jonathan Hart | f84591d | 2015-01-16 14:33:43 -0800 | [diff] [blame] | 416 | private void flood(Ethernet request, ConnectPoint inPort) { |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 417 | TrafficTreatment.Builder builder = null; |
| 418 | ByteBuffer buf = ByteBuffer.wrap(request.serialize()); |
| 419 | |
Aaron Kruglikov | d812383 | 2015-07-06 14:20:25 -0700 | [diff] [blame] | 420 | for (ConnectPoint connectPoint : edgeService.getEdgePoints()) { |
| 421 | if (isOutsidePort(connectPoint) || connectPoint.equals(inPort)) { |
| 422 | continue; |
| 423 | } |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 424 | |
Aaron Kruglikov | d812383 | 2015-07-06 14:20:25 -0700 | [diff] [blame] | 425 | builder = DefaultTrafficTreatment.builder(); |
| 426 | builder.setOutput(connectPoint.port()); |
| 427 | packetService.emit(new DefaultOutboundPacket(connectPoint.deviceId(), |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame^] | 428 | builder.build(), buf)); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | } |
| 432 | |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 433 | /** |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 434 | * Builds an Neighbor Discovery reply based on a request. |
| 435 | * |
Aaron Kruglikov | d812383 | 2015-07-06 14:20:25 -0700 | [diff] [blame] | 436 | * @param srcIp the IP address to use as the reply source |
| 437 | * @param srcMac the MAC address to use as the reply source |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 438 | * @param request the Neighbor Solicitation request we got |
| 439 | * @return an Ethernet frame containing the Neighbor Advertisement reply |
| 440 | */ |
| 441 | private Ethernet buildNdpReply(Ip6Address srcIp, MacAddress srcMac, |
| 442 | Ethernet request) { |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 443 | Ethernet eth = new Ethernet(); |
| 444 | eth.setDestinationMACAddress(request.getSourceMAC()); |
| 445 | eth.setSourceMACAddress(srcMac); |
| 446 | eth.setEtherType(Ethernet.TYPE_IPV6); |
| 447 | eth.setVlanID(request.getVlanID()); |
| 448 | |
| 449 | IPv6 requestIp = (IPv6) request.getPayload(); |
| 450 | IPv6 ipv6 = new IPv6(); |
| 451 | ipv6.setSourceAddress(srcIp.toOctets()); |
| 452 | ipv6.setDestinationAddress(requestIp.getSourceAddress()); |
| 453 | ipv6.setHopLimit((byte) 255); |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 454 | |
| 455 | ICMP6 icmp6 = new ICMP6(); |
| 456 | icmp6.setIcmpType(ICMP6.NEIGHBOR_ADVERTISEMENT); |
| 457 | icmp6.setIcmpCode((byte) 0); |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 458 | |
| 459 | NeighborAdvertisement nadv = new NeighborAdvertisement(); |
Dusan Pajin | a22b970 | 2015-02-12 16:25:23 +0100 | [diff] [blame] | 460 | nadv.setTargetAddress(srcIp.toOctets()); |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 461 | nadv.setSolicitedFlag((byte) 1); |
| 462 | nadv.setOverrideFlag((byte) 1); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 463 | nadv.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame^] | 464 | srcMac.toBytes()); |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 465 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 466 | icmp6.setPayload(nadv); |
Dusan Pajin | a22b970 | 2015-02-12 16:25:23 +0100 | [diff] [blame] | 467 | ipv6.setPayload(icmp6); |
| 468 | eth.setPayload(ipv6); |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 469 | return eth; |
| 470 | } |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 471 | } |