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; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 36 | import org.onosproject.incubator.net.intf.Interface; |
| 37 | import org.onosproject.incubator.net.intf.InterfaceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.net.ConnectPoint; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 39 | import org.onosproject.net.Host; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 40 | import org.onosproject.net.device.DeviceService; |
Aaron Kruglikov | d812383 | 2015-07-06 14:20:25 -0700 | [diff] [blame] | 41 | import org.onosproject.net.edge.EdgePortService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 42 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 43 | import org.onosproject.net.flow.TrafficTreatment; |
| 44 | import org.onosproject.net.host.HostService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 45 | import org.onosproject.net.link.LinkService; |
| 46 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 47 | import org.onosproject.net.packet.InboundPacket; |
| 48 | import org.onosproject.net.packet.PacketContext; |
| 49 | import org.onosproject.net.packet.PacketService; |
| 50 | import org.onosproject.net.proxyarp.ProxyArpService; |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 51 | import org.onosproject.net.proxyarp.ProxyArpStore; |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 52 | import org.slf4j.Logger; |
| 53 | |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 54 | import java.nio.ByteBuffer; |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 55 | import java.util.Set; |
| 56 | |
| 57 | import static com.google.common.base.Preconditions.checkArgument; |
| 58 | import static com.google.common.base.Preconditions.checkNotNull; |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 59 | import static org.onlab.packet.VlanId.vlanId; |
| 60 | import static org.onosproject.net.HostId.hostId; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 61 | import static org.onosproject.security.AppGuard.checkPermission; |
Aaron Kruglikov | d812383 | 2015-07-06 14:20:25 -0700 | [diff] [blame] | 62 | import static org.slf4j.LoggerFactory.getLogger; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 63 | import static org.onosproject.security.AppPermission.Type.*; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 64 | |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 65 | |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 66 | @Component(immediate = true) |
| 67 | @Service |
| 68 | public class ProxyArpManager implements ProxyArpService { |
| 69 | |
| 70 | private final Logger log = getLogger(getClass()); |
| 71 | |
Jonathan Hart | 39ee648 | 2015-08-31 16:00:19 +0200 | [diff] [blame] | 72 | private static final String MAC_ADDR_NULL = "MAC address cannot be null."; |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 73 | private static final String REQUEST_NULL = "ARP or NDP request cannot be null."; |
Jonathan Hart | 39ee648 | 2015-08-31 16:00:19 +0200 | [diff] [blame] | 74 | private static final String MSG_NOT_REQUEST = "Message is not an ARP or NDP request"; |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 75 | |
| 76 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Aaron Kruglikov | d812383 | 2015-07-06 14:20:25 -0700 | [diff] [blame] | 77 | protected EdgePortService edgeService; |
| 78 | |
| 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 80 | protected HostService hostService; |
| 81 | |
| 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 83 | protected PacketService packetService; |
| 84 | |
| 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 86 | protected LinkService linkService; |
| 87 | |
| 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 89 | protected DeviceService deviceService; |
| 90 | |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 92 | protected ProxyArpStore store; |
| 93 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 94 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 95 | protected InterfaceService interfaceService; |
| 96 | |
Jonathan Hart | 39ee648 | 2015-08-31 16:00:19 +0200 | [diff] [blame] | 97 | private enum Protocol { |
| 98 | ARP, NDP |
| 99 | } |
| 100 | |
| 101 | private enum MessageType { |
| 102 | REQUEST, REPLY |
| 103 | } |
| 104 | |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 105 | @Activate |
| 106 | public void activate() { |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 107 | store.setDelegate(this::sendTo); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 108 | log.info("Started"); |
| 109 | } |
| 110 | |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 111 | @Deactivate |
| 112 | public void deactivate() { |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 113 | store.setDelegate(null); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 114 | log.info("Stopped"); |
| 115 | } |
| 116 | |
| 117 | @Override |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 118 | public boolean isKnown(IpAddress addr) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 119 | checkPermission(PACKET_READ); |
| 120 | |
Yuta HIGUCHI | 5971804 | 2014-10-04 22:04:56 -0700 | [diff] [blame] | 121 | checkNotNull(addr, MAC_ADDR_NULL); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 122 | Set<Host> hosts = hostService.getHostsByIp(addr); |
| 123 | return !hosts.isEmpty(); |
| 124 | } |
| 125 | |
| 126 | @Override |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 127 | public void reply(Ethernet eth, ConnectPoint inPort) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 128 | checkPermission(PACKET_WRITE); |
| 129 | |
Yuta HIGUCHI | 5971804 | 2014-10-04 22:04:56 -0700 | [diff] [blame] | 130 | checkNotNull(eth, REQUEST_NULL); |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 131 | |
Jonathan Hart | 39ee648 | 2015-08-31 16:00:19 +0200 | [diff] [blame] | 132 | MessageContext context = createContext(eth, inPort); |
| 133 | if (context != null) { |
| 134 | replyInternal(context); |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
Jonathan Hart | 39ee648 | 2015-08-31 16:00:19 +0200 | [diff] [blame] | 138 | /** |
| 139 | * Handles a request message. |
| 140 | * |
| 141 | * If the MAC address of the target is known, we can reply directly to the |
| 142 | * requestor. Otherwise, we forward the request out other ports in an |
| 143 | * attempt to find the correct host. |
| 144 | * |
| 145 | * @param context request message context to process |
| 146 | */ |
| 147 | private void replyInternal(MessageContext context) { |
| 148 | checkNotNull(context); |
| 149 | checkArgument(context.type() == MessageType.REQUEST, MSG_NOT_REQUEST); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 150 | |
Jonathan Hart | 39ee648 | 2015-08-31 16:00:19 +0200 | [diff] [blame] | 151 | if (hasIpAddress(context.inPort())) { |
Jonathan Hart | 7d1496b | 2015-03-10 22:00:48 -0700 | [diff] [blame] | 152 | // If the request came from outside the network, only reply if it was |
| 153 | // for one of our external addresses. |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 154 | |
Jonathan Hart | 39ee648 | 2015-08-31 16:00:19 +0200 | [diff] [blame] | 155 | interfaceService.getInterfacesByPort(context.inPort()) |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 156 | .stream() |
| 157 | .filter(intf -> intf.ipAddresses() |
| 158 | .stream() |
Jonathan Hart | 39ee648 | 2015-08-31 16:00:19 +0200 | [diff] [blame] | 159 | .anyMatch(ia -> ia.ipAddress().equals(context.target()))) |
| 160 | .forEach(intf -> buildAndSendReply(context, intf.mac())); |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 161 | |
| 162 | // Stop here and don't proxy ARPs if the port has an IP address |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 163 | return; |
| 164 | } |
| 165 | |
Jonathan Hart | 7d1496b | 2015-03-10 22:00:48 -0700 | [diff] [blame] | 166 | // See if we have the target host in the host store |
Jonathan Hart | 39ee648 | 2015-08-31 16:00:19 +0200 | [diff] [blame] | 167 | Set<Host> hosts = hostService.getHostsByIp(context.target()); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 168 | |
| 169 | Host dst = null; |
Jonathan Hart | 39ee648 | 2015-08-31 16:00:19 +0200 | [diff] [blame] | 170 | Host src = hostService.getHost(hostId(context.srcMac(), context.vlan())); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 171 | |
| 172 | for (Host host : hosts) { |
Jonathan Hart | 39ee648 | 2015-08-31 16:00:19 +0200 | [diff] [blame] | 173 | if (host.vlan().equals(context.vlan())) { |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 174 | dst = host; |
| 175 | break; |
| 176 | } |
| 177 | } |
| 178 | |
Jonathan Hart | 7d1496b | 2015-03-10 22:00:48 -0700 | [diff] [blame] | 179 | if (src != null && dst != null) { |
| 180 | // We know the target host so we can respond |
Jonathan Hart | 39ee648 | 2015-08-31 16:00:19 +0200 | [diff] [blame] | 181 | buildAndSendReply(context, dst.mac()); |
Jonathan Hart | 7d1496b | 2015-03-10 22:00:48 -0700 | [diff] [blame] | 182 | return; |
| 183 | } |
| 184 | |
| 185 | // If the source address matches one of our external addresses |
| 186 | // it could be a request from an internal host to an external |
| 187 | // address. Forward it over to the correct port. |
Jonathan Hart | 7d1496b | 2015-03-10 22:00:48 -0700 | [diff] [blame] | 188 | boolean matched = false; |
Jonathan Hart | 39ee648 | 2015-08-31 16:00:19 +0200 | [diff] [blame] | 189 | Set<Interface> interfaces = interfaceService.getInterfacesByIp(context.sender()); |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 190 | for (Interface intf : interfaces) { |
Jonathan Hart | 39ee648 | 2015-08-31 16:00:19 +0200 | [diff] [blame] | 191 | if (intf.vlan().equals(context.vlan())) { |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 192 | matched = true; |
Jonathan Hart | 39ee648 | 2015-08-31 16:00:19 +0200 | [diff] [blame] | 193 | sendTo(context.packet(), intf.connectPoint()); |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 194 | break; |
Jonathan Hart | 7d1496b | 2015-03-10 22:00:48 -0700 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
| 198 | if (matched) { |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 199 | return; |
| 200 | } |
| 201 | |
Jonathan Hart | 7d1496b | 2015-03-10 22:00:48 -0700 | [diff] [blame] | 202 | // The request couldn't be resolved. |
| 203 | // Flood the request on all ports except the incoming port. |
Jonathan Hart | 39ee648 | 2015-08-31 16:00:19 +0200 | [diff] [blame] | 204 | flood(context.packet(), context.inPort()); |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 205 | } |
| 206 | |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 207 | /** |
Jonathan Hart | 39ee648 | 2015-08-31 16:00:19 +0200 | [diff] [blame] | 208 | * Builds and sends a reply message given a request context and the resolved |
| 209 | * MAC address to answer with. |
| 210 | * |
| 211 | * @param context message context of request |
| 212 | * @param targetMac MAC address to be given in the response |
| 213 | */ |
| 214 | private void buildAndSendReply(MessageContext context, MacAddress targetMac) { |
| 215 | switch (context.protocol()) { |
| 216 | case ARP: |
| 217 | sendTo(ARP.buildArpReply((Ip4Address) context.target(), |
| 218 | targetMac, context.packet()), context.inPort()); |
| 219 | break; |
| 220 | case NDP: |
| 221 | sendTo(buildNdpReply((Ip6Address) context.target(), targetMac, |
| 222 | context.packet()), context.inPort()); |
| 223 | break; |
| 224 | default: |
| 225 | break; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Outputs a packet out a specific port. |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 231 | * |
Aaron Kruglikov | d812383 | 2015-07-06 14:20:25 -0700 | [diff] [blame] | 232 | * @param packet the packet to send |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 233 | * @param outPort the port to send it out |
| 234 | */ |
| 235 | private void sendTo(Ethernet packet, ConnectPoint outPort) { |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 236 | sendTo(outPort, ByteBuffer.wrap(packet.serialize())); |
| 237 | } |
| 238 | |
Jonathan Hart | 39ee648 | 2015-08-31 16:00:19 +0200 | [diff] [blame] | 239 | /** |
| 240 | * Outputs a packet out a specific port. |
| 241 | * |
| 242 | * @param outPort port to send it out |
| 243 | * @param packet packet to send |
| 244 | */ |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 245 | private void sendTo(ConnectPoint outPort, ByteBuffer packet) { |
Aaron Kruglikov | d812383 | 2015-07-06 14:20:25 -0700 | [diff] [blame] | 246 | if (!edgeService.isEdgePoint(outPort)) { |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 247 | // Sanity check to make sure we don't send the packet out an |
| 248 | // internal port and create a loop (could happen due to |
| 249 | // misconfiguration). |
| 250 | return; |
| 251 | } |
| 252 | |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 253 | TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder(); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 254 | builder.setOutput(outPort.port()); |
| 255 | packetService.emit(new DefaultOutboundPacket(outPort.deviceId(), |
Jonathan Hart | 39ee648 | 2015-08-31 16:00:19 +0200 | [diff] [blame] | 256 | builder.build(), packet)); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /** |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 260 | * Returns whether the given port has any IP addresses configured or not. |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 261 | * |
| 262 | * @param port the port to check |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 263 | * @return true if the port has at least one IP address configured, |
| 264 | * otherwise false |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 265 | */ |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 266 | private boolean hasIpAddress(ConnectPoint port) { |
| 267 | return interfaceService.getInterfacesByPort(port) |
| 268 | .stream() |
| 269 | .map(intf -> intf.ipAddresses()) |
| 270 | .findAny() |
| 271 | .isPresent(); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | @Override |
Jonathan Hart | f84591d | 2015-01-16 14:33:43 -0800 | [diff] [blame] | 275 | public void forward(Ethernet eth, ConnectPoint inPort) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 276 | checkPermission(PACKET_WRITE); |
| 277 | |
Yuta HIGUCHI | 5971804 | 2014-10-04 22:04:56 -0700 | [diff] [blame] | 278 | checkNotNull(eth, REQUEST_NULL); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 279 | |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 280 | Host h = hostService.getHost(hostId(eth.getDestinationMAC(), |
Jonathan Hart | 39ee648 | 2015-08-31 16:00:19 +0200 | [diff] [blame] | 281 | vlanId(eth.getVlanID()))); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 282 | |
| 283 | if (h == null) { |
Jonathan Hart | f84591d | 2015-01-16 14:33:43 -0800 | [diff] [blame] | 284 | flood(eth, inPort); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 285 | } else { |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 286 | Host subject = hostService.getHost(hostId(eth.getSourceMAC(), |
| 287 | vlanId(eth.getVlanID()))); |
| 288 | store.forward(h.location(), subject, ByteBuffer.wrap(eth.serialize())); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 289 | } |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 290 | } |
| 291 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 292 | @Override |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 293 | public boolean handlePacket(PacketContext context) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 294 | checkPermission(PACKET_WRITE); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 295 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 296 | InboundPacket pkt = context.inPacket(); |
| 297 | Ethernet ethPkt = pkt.parsed(); |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 298 | |
| 299 | if (ethPkt == null) { |
| 300 | return false; |
| 301 | } |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 302 | |
Jonathan Hart | 39ee648 | 2015-08-31 16:00:19 +0200 | [diff] [blame] | 303 | MessageContext msgContext = createContext(ethPkt, pkt.receivedFrom()); |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 304 | |
Jonathan Hart | 39ee648 | 2015-08-31 16:00:19 +0200 | [diff] [blame] | 305 | if (msgContext == null) { |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 306 | return false; |
| 307 | } |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 308 | |
Jonathan Hart | 39ee648 | 2015-08-31 16:00:19 +0200 | [diff] [blame] | 309 | switch (msgContext.type()) { |
| 310 | case REPLY: |
| 311 | forward(msgContext.packet(), msgContext.inPort()); |
| 312 | break; |
| 313 | case REQUEST: |
| 314 | replyInternal(msgContext); |
| 315 | break; |
| 316 | default: |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 317 | return false; |
| 318 | } |
Jonathan Hart | 39ee648 | 2015-08-31 16:00:19 +0200 | [diff] [blame] | 319 | |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 320 | context.block(); |
| 321 | return true; |
| 322 | } |
| 323 | |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 324 | /** |
| 325 | * Flood the arp request at all edges in the network. |
Dusan Pajin | a22b970 | 2015-02-12 16:25:23 +0100 | [diff] [blame] | 326 | * |
| 327 | * @param request the arp request |
Aaron Kruglikov | d812383 | 2015-07-06 14:20:25 -0700 | [diff] [blame] | 328 | * @param inPort the connect point the arp request was received on |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 329 | */ |
Jonathan Hart | f84591d | 2015-01-16 14:33:43 -0800 | [diff] [blame] | 330 | private void flood(Ethernet request, ConnectPoint inPort) { |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 331 | TrafficTreatment.Builder builder = null; |
| 332 | ByteBuffer buf = ByteBuffer.wrap(request.serialize()); |
| 333 | |
Aaron Kruglikov | d812383 | 2015-07-06 14:20:25 -0700 | [diff] [blame] | 334 | for (ConnectPoint connectPoint : edgeService.getEdgePoints()) { |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 335 | if (hasIpAddress(connectPoint) || connectPoint.equals(inPort)) { |
Aaron Kruglikov | d812383 | 2015-07-06 14:20:25 -0700 | [diff] [blame] | 336 | continue; |
| 337 | } |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 338 | |
Aaron Kruglikov | d812383 | 2015-07-06 14:20:25 -0700 | [diff] [blame] | 339 | builder = DefaultTrafficTreatment.builder(); |
| 340 | builder.setOutput(connectPoint.port()); |
| 341 | packetService.emit(new DefaultOutboundPacket(connectPoint.deviceId(), |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 342 | builder.build(), buf)); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 343 | } |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 344 | } |
| 345 | |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 346 | /** |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 347 | * Builds an Neighbor Discovery reply based on a request. |
| 348 | * |
Aaron Kruglikov | d812383 | 2015-07-06 14:20:25 -0700 | [diff] [blame] | 349 | * @param srcIp the IP address to use as the reply source |
| 350 | * @param srcMac the MAC address to use as the reply source |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 351 | * @param request the Neighbor Solicitation request we got |
| 352 | * @return an Ethernet frame containing the Neighbor Advertisement reply |
| 353 | */ |
| 354 | private Ethernet buildNdpReply(Ip6Address srcIp, MacAddress srcMac, |
| 355 | Ethernet request) { |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 356 | Ethernet eth = new Ethernet(); |
| 357 | eth.setDestinationMACAddress(request.getSourceMAC()); |
| 358 | eth.setSourceMACAddress(srcMac); |
| 359 | eth.setEtherType(Ethernet.TYPE_IPV6); |
| 360 | eth.setVlanID(request.getVlanID()); |
| 361 | |
| 362 | IPv6 requestIp = (IPv6) request.getPayload(); |
| 363 | IPv6 ipv6 = new IPv6(); |
| 364 | ipv6.setSourceAddress(srcIp.toOctets()); |
| 365 | ipv6.setDestinationAddress(requestIp.getSourceAddress()); |
| 366 | ipv6.setHopLimit((byte) 255); |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 367 | |
| 368 | ICMP6 icmp6 = new ICMP6(); |
| 369 | icmp6.setIcmpType(ICMP6.NEIGHBOR_ADVERTISEMENT); |
| 370 | icmp6.setIcmpCode((byte) 0); |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 371 | |
| 372 | NeighborAdvertisement nadv = new NeighborAdvertisement(); |
Dusan Pajin | a22b970 | 2015-02-12 16:25:23 +0100 | [diff] [blame] | 373 | nadv.setTargetAddress(srcIp.toOctets()); |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 374 | nadv.setSolicitedFlag((byte) 1); |
| 375 | nadv.setOverrideFlag((byte) 1); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 376 | nadv.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 377 | srcMac.toBytes()); |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 378 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 379 | icmp6.setPayload(nadv); |
Dusan Pajin | a22b970 | 2015-02-12 16:25:23 +0100 | [diff] [blame] | 380 | ipv6.setPayload(icmp6); |
| 381 | eth.setPayload(ipv6); |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 382 | return eth; |
| 383 | } |
Jonathan Hart | 39ee648 | 2015-08-31 16:00:19 +0200 | [diff] [blame] | 384 | |
| 385 | /** |
| 386 | * Attempts to create a MessageContext for the given Ethernet frame. If the |
| 387 | * frame is a valid ARP or NDP request or response, a context will be |
| 388 | * created. |
| 389 | * |
| 390 | * @param eth input Ethernet frame |
| 391 | * @param inPort in port |
| 392 | * @return MessageContext if the packet was ARP or NDP, otherwise null |
| 393 | */ |
| 394 | private MessageContext createContext(Ethernet eth, ConnectPoint inPort) { |
| 395 | if (eth.getEtherType() == Ethernet.TYPE_ARP) { |
| 396 | return createArpContext(eth, inPort); |
| 397 | } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) { |
| 398 | return createNdpContext(eth, inPort); |
| 399 | } |
| 400 | |
| 401 | return null; |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Extracts context information from ARP packets. |
| 406 | * |
| 407 | * @param eth input Ethernet frame that is thought to be ARP |
| 408 | * @param inPort in port |
| 409 | * @return MessageContext object if the packet was a valid ARP packet, |
| 410 | * otherwise null |
| 411 | */ |
| 412 | private MessageContext createArpContext(Ethernet eth, ConnectPoint inPort) { |
| 413 | if (eth.getEtherType() != Ethernet.TYPE_ARP) { |
| 414 | return null; |
| 415 | } |
| 416 | |
| 417 | ARP arp = (ARP) eth.getPayload(); |
| 418 | |
| 419 | IpAddress target = Ip4Address.valueOf(arp.getTargetProtocolAddress()); |
| 420 | IpAddress sender = Ip4Address.valueOf(arp.getSenderProtocolAddress()); |
| 421 | |
| 422 | MessageType type; |
| 423 | if (arp.getOpCode() == ARP.OP_REQUEST) { |
| 424 | type = MessageType.REQUEST; |
| 425 | } else if (arp.getOpCode() == ARP.OP_REPLY) { |
| 426 | type = MessageType.REPLY; |
| 427 | } else { |
| 428 | return null; |
| 429 | } |
| 430 | |
| 431 | return new MessageContext(eth, inPort, Protocol.ARP, type, target, sender); |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Extracts context information from NDP packets. |
| 436 | * |
| 437 | * @param eth input Ethernet frame that is thought to be NDP |
| 438 | * @param inPort in port |
| 439 | * @return MessageContext object if the packet was a valid NDP packet, |
| 440 | * otherwise null |
| 441 | */ |
| 442 | private MessageContext createNdpContext(Ethernet eth, ConnectPoint inPort) { |
| 443 | if (eth.getEtherType() != Ethernet.TYPE_IPV6) { |
| 444 | return null; |
| 445 | } |
| 446 | IPv6 ipv6 = (IPv6) eth.getPayload(); |
| 447 | |
| 448 | if (ipv6.getNextHeader() != IPv6.PROTOCOL_ICMP6) { |
| 449 | return null; |
| 450 | } |
| 451 | ICMP6 icmpv6 = (ICMP6) ipv6.getPayload(); |
| 452 | |
| 453 | IpAddress sender = Ip6Address.valueOf(ipv6.getSourceAddress()); |
| 454 | IpAddress target = null; |
| 455 | |
| 456 | MessageType type; |
| 457 | if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_SOLICITATION) { |
| 458 | type = MessageType.REQUEST; |
| 459 | NeighborSolicitation nsol = (NeighborSolicitation) icmpv6.getPayload(); |
| 460 | target = Ip6Address.valueOf(nsol.getTargetAddress()); |
| 461 | } else if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_ADVERTISEMENT) { |
| 462 | type = MessageType.REPLY; |
| 463 | } else { |
| 464 | return null; |
| 465 | } |
| 466 | |
| 467 | return new MessageContext(eth, inPort, Protocol.NDP, type, target, sender); |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * Provides context information for a particular ARP or NDP message, with |
| 472 | * a unified interface to access data regardless of protocol. |
| 473 | */ |
| 474 | private class MessageContext { |
| 475 | private Protocol protocol; |
| 476 | private MessageType type; |
| 477 | |
| 478 | private IpAddress target; |
| 479 | private IpAddress sender; |
| 480 | |
| 481 | private Ethernet eth; |
| 482 | private ConnectPoint inPort; |
| 483 | |
| 484 | |
| 485 | public MessageContext(Ethernet eth, ConnectPoint inPort, |
| 486 | Protocol protocol, MessageType type, |
| 487 | IpAddress target, IpAddress sender) { |
| 488 | this.eth = eth; |
| 489 | this.inPort = inPort; |
| 490 | this.protocol = protocol; |
| 491 | this.type = type; |
| 492 | this.target = target; |
| 493 | this.sender = sender; |
| 494 | } |
| 495 | |
| 496 | public ConnectPoint inPort() { |
| 497 | return inPort; |
| 498 | } |
| 499 | |
| 500 | public Ethernet packet() { |
| 501 | return eth; |
| 502 | } |
| 503 | |
| 504 | public Protocol protocol() { |
| 505 | return protocol; |
| 506 | } |
| 507 | |
| 508 | public MessageType type() { |
| 509 | return type; |
| 510 | } |
| 511 | |
| 512 | public VlanId vlan() { |
| 513 | return VlanId.vlanId(eth.getVlanID()); |
| 514 | } |
| 515 | |
| 516 | public MacAddress srcMac() { |
| 517 | return MacAddress.valueOf(eth.getSourceMACAddress()); |
| 518 | } |
| 519 | |
| 520 | public IpAddress target() { |
| 521 | return target; |
| 522 | } |
| 523 | |
| 524 | public IpAddress sender() { |
| 525 | return sender; |
| 526 | } |
| 527 | } |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 528 | } |