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