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