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