sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 43b5354 | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
sangho | 80f11cb | 2015-04-01 13:05:26 -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 | */ |
| 16 | package org.onosproject.segmentrouting; |
| 17 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 18 | import org.onlab.packet.Ethernet; |
| 19 | import org.onlab.packet.ICMP; |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 20 | import org.onlab.packet.ICMP6; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 21 | import org.onlab.packet.IPv4; |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 22 | import org.onlab.packet.IPv6; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 23 | import org.onlab.packet.Ip4Address; |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 24 | import org.onlab.packet.Ip6Address; |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 25 | import org.onlab.packet.IpAddress; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 26 | import org.onlab.packet.MPLS; |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 27 | import org.onlab.packet.MacAddress; |
| 28 | import org.onlab.packet.VlanId; |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 29 | import org.onlab.packet.ndp.NeighborSolicitation; |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 30 | import org.onosproject.incubator.net.neighbour.NeighbourMessageContext; |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 31 | import org.onosproject.incubator.net.neighbour.NeighbourMessageType; |
Charles Chan | b8caeab | 2017-06-02 19:23:51 -0700 | [diff] [blame^] | 32 | import org.onosproject.incubator.net.routing.ResolvedRoute; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 33 | import org.onosproject.net.ConnectPoint; |
| 34 | import org.onosproject.net.DeviceId; |
| 35 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 36 | import org.onosproject.net.flow.TrafficTreatment; |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 37 | import org.onosproject.net.host.HostService; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 38 | import org.onosproject.net.packet.DefaultOutboundPacket; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 39 | import org.onosproject.net.packet.OutboundPacket; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 40 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 41 | import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 42 | import org.slf4j.Logger; |
| 43 | import org.slf4j.LoggerFactory; |
| 44 | |
Jonathan Hart | d53ebc4 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 45 | import java.nio.ByteBuffer; |
Charles Chan | d3727b7 | 2017-03-13 13:10:30 -0700 | [diff] [blame] | 46 | import java.util.Arrays; |
Charles Chan | b8caeab | 2017-06-02 19:23:51 -0700 | [diff] [blame^] | 47 | import java.util.Optional; |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 48 | import java.util.Set; |
Jonathan Hart | d53ebc4 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 49 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 50 | /** |
| 51 | * Handler of ICMP packets that responses or forwards ICMP packets that |
| 52 | * are sent to the controller. |
| 53 | */ |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 54 | public class IcmpHandler extends SegmentRoutingNeighbourHandler { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 55 | |
| 56 | private static Logger log = LoggerFactory.getLogger(IcmpHandler.class); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Creates an IcmpHandler object. |
| 60 | * |
| 61 | * @param srManager SegmentRoutingManager object |
| 62 | */ |
| 63 | public IcmpHandler(SegmentRoutingManager srManager) { |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 64 | super(srManager); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Utility function to send packet out. |
| 69 | * |
| 70 | * @param outport the output port |
| 71 | * @param payload the packet to send |
| 72 | * @param sid the segment id |
| 73 | * @param destIpAddress the destination ip address |
| 74 | * @param allowedHops the hop limit/ttl |
| 75 | */ |
| 76 | private void sendPacketOut(ConnectPoint outport, |
| 77 | Ethernet payload, |
| 78 | int sid, |
| 79 | IpAddress destIpAddress, |
| 80 | byte allowedHops) { |
| 81 | int destSid; |
| 82 | if (destIpAddress.isIp4()) { |
| 83 | destSid = config.getIPv4SegmentId(payload.getDestinationMAC()); |
| 84 | } else { |
| 85 | destSid = config.getIPv6SegmentId(payload.getDestinationMAC()); |
| 86 | } |
| 87 | |
| 88 | if (sid == -1 || destSid == sid || |
| 89 | config.inSameSubnet(outport.deviceId(), destIpAddress)) { |
| 90 | TrafficTreatment treatment = DefaultTrafficTreatment.builder(). |
| 91 | setOutput(outport.port()).build(); |
| 92 | OutboundPacket packet = new DefaultOutboundPacket(outport.deviceId(), |
| 93 | treatment, ByteBuffer.wrap(payload.serialize())); |
| 94 | srManager.packetService.emit(packet); |
| 95 | } else { |
| 96 | log.debug("Send a MPLS packet as a ICMP response"); |
| 97 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 98 | .setOutput(outport.port()) |
| 99 | .build(); |
| 100 | |
| 101 | payload.setEtherType(Ethernet.MPLS_UNICAST); |
| 102 | MPLS mplsPkt = new MPLS(); |
| 103 | mplsPkt.setLabel(sid); |
| 104 | mplsPkt.setTtl(allowedHops); |
| 105 | mplsPkt.setPayload(payload.getPayload()); |
| 106 | payload.setPayload(mplsPkt); |
| 107 | |
| 108 | OutboundPacket packet = new DefaultOutboundPacket(outport.deviceId(), |
| 109 | treatment, ByteBuffer.wrap(payload.serialize())); |
| 110 | |
| 111 | srManager.packetService.emit(packet); |
| 112 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 115 | ////////////////////////////////////// |
| 116 | // ICMP Echo/Reply Protocol // |
| 117 | ////////////////////////////////////// |
| 118 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 119 | /** |
| 120 | * Process incoming ICMP packet. |
Charles Chan | b78b113 | 2017-03-29 17:24:39 -0700 | [diff] [blame] | 121 | * If it is an ICMP request to router, then sends an ICMP response. |
| 122 | * Otherwise ignore the packet. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 123 | * |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 124 | * @param eth inbound ICMP packet |
| 125 | * @param inPort the input port |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 126 | */ |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 127 | public void processIcmp(Ethernet eth, ConnectPoint inPort) { |
| 128 | DeviceId deviceId = inPort.deviceId(); |
| 129 | IPv4 ipv4Packet = (IPv4) eth.getPayload(); |
| 130 | Ip4Address destinationAddress = Ip4Address.valueOf(ipv4Packet.getDestinationAddress()); |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 131 | Set<IpAddress> gatewayIpAddresses = config.getPortIPs(deviceId); |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 132 | IpAddress routerIp; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 133 | try { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 134 | routerIp = config.getRouterIpv4(deviceId); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 135 | } catch (DeviceConfigNotFoundException e) { |
| 136 | log.warn(e.getMessage() + " Aborting processPacketIn."); |
| 137 | return; |
| 138 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 139 | // ICMP to the router IP or gateway IP |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 140 | if (((ICMP) ipv4Packet.getPayload()).getIcmpType() == ICMP.TYPE_ECHO_REQUEST && |
| 141 | (destinationAddress.equals(routerIp.getIp4Address()) || |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 142 | gatewayIpAddresses.contains(destinationAddress))) { |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 143 | sendIcmpResponse(eth, inPort); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 144 | } else { |
Charles Chan | b78b113 | 2017-03-29 17:24:39 -0700 | [diff] [blame] | 145 | log.trace("Ignore ICMP that targets for {}", destinationAddress); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 146 | } |
Charles Chan | b78b113 | 2017-03-29 17:24:39 -0700 | [diff] [blame] | 147 | // We remove the packet from the queue |
| 148 | srManager.ipHandler.dequeuePacket(ipv4Packet, destinationAddress); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 151 | /** |
| 152 | * Sends an ICMP reply message. |
| 153 | * |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 154 | * @param icmpRequest the original ICMP request |
| 155 | * @param outport the output port where the ICMP reply should be sent to |
| 156 | */ |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 157 | private void sendIcmpResponse(Ethernet icmpRequest, ConnectPoint outport) { |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 158 | Ethernet icmpReplyEth = ICMP.buildIcmpReply(icmpRequest); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 159 | IPv4 icmpRequestIpv4 = (IPv4) icmpRequest.getPayload(); |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 160 | IPv4 icmpReplyIpv4 = (IPv4) icmpReplyEth.getPayload(); |
| 161 | Ip4Address destIpAddress = Ip4Address.valueOf(icmpRequestIpv4.getSourceAddress()); |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 162 | Ip4Address destRouterAddress = config.getRouterIpAddressForASubnetHost(destIpAddress); |
Charles Chan | b8caeab | 2017-06-02 19:23:51 -0700 | [diff] [blame^] | 163 | |
| 164 | // Note: Source IP of the ICMP request doesn't belong to any configured subnet. |
| 165 | // The source might be an indirectly attached host (e.g. behind a router) |
| 166 | // Lookup the route store for the nexthop instead. |
| 167 | if (destRouterAddress == null) { |
| 168 | Optional<ResolvedRoute> nexthop = srManager.routeService.longestPrefixLookup(destIpAddress); |
| 169 | if (nexthop.isPresent()) { |
| 170 | try { |
| 171 | destRouterAddress = config.getRouterIpv4(nexthop.get().location().deviceId()); |
| 172 | } catch (DeviceConfigNotFoundException e) { |
| 173 | log.warn("Device config not found. Abort ICMP processing"); |
| 174 | return; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 179 | int destSid = config.getIPv4SegmentId(destRouterAddress); |
Charles Chan | 7066136 | 2016-12-09 12:54:49 -0800 | [diff] [blame] | 180 | if (destSid < 0) { |
Charles Chan | b8caeab | 2017-06-02 19:23:51 -0700 | [diff] [blame^] | 181 | log.warn("Failed to lookup SID of the switch that {} attaches to. " + |
| 182 | "Unable to process ICMP request.", destIpAddress); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 183 | return; |
| 184 | } |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 185 | sendPacketOut(outport, icmpReplyEth, destSid, destIpAddress, icmpReplyIpv4.getTtl()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 188 | /////////////////////////////////////////// |
| 189 | // ICMPv6 Echo/Reply Protocol // |
| 190 | /////////////////////////////////////////// |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 191 | |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 192 | /** |
| 193 | * Process incoming ICMPv6 packet. |
Charles Chan | b78b113 | 2017-03-29 17:24:39 -0700 | [diff] [blame] | 194 | * If it is an ICMPv6 request to router, then sends an ICMPv6 response. |
| 195 | * Otherwise ignore the packet. |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 196 | * |
| 197 | * @param eth the incoming ICMPv6 packet |
| 198 | * @param inPort the input port |
| 199 | */ |
| 200 | public void processIcmpv6(Ethernet eth, ConnectPoint inPort) { |
| 201 | DeviceId deviceId = inPort.deviceId(); |
| 202 | IPv6 ipv6Packet = (IPv6) eth.getPayload(); |
| 203 | Ip6Address destinationAddress = Ip6Address.valueOf(ipv6Packet.getDestinationAddress()); |
| 204 | Set<IpAddress> gatewayIpAddresses = config.getPortIPs(deviceId); |
| 205 | IpAddress routerIp; |
| 206 | try { |
| 207 | routerIp = config.getRouterIpv6(deviceId); |
| 208 | } catch (DeviceConfigNotFoundException e) { |
| 209 | log.warn(e.getMessage() + " Aborting processPacketIn."); |
| 210 | return; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 211 | } |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 212 | ICMP6 icmp6 = (ICMP6) ipv6Packet.getPayload(); |
| 213 | // ICMP to the router IP or gateway IP |
| 214 | if (icmp6.getIcmpType() == ICMP6.ECHO_REQUEST && |
| 215 | (destinationAddress.equals(routerIp.getIp6Address()) || |
| 216 | gatewayIpAddresses.contains(destinationAddress))) { |
| 217 | sendIcmpv6Response(eth, inPort); |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 218 | } else { |
Charles Chan | b78b113 | 2017-03-29 17:24:39 -0700 | [diff] [blame] | 219 | log.trace("Ignore ICMPv6 that targets for {}", destinationAddress); |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Sends an ICMPv6 reply message. |
| 225 | * |
| 226 | * Note: we assume that packets sending from the edge switches to the hosts |
| 227 | * have untagged VLAN. |
| 228 | * @param ethRequest the original ICMP request |
| 229 | * @param outport the output port where the ICMP reply should be sent to |
| 230 | */ |
| 231 | private void sendIcmpv6Response(Ethernet ethRequest, ConnectPoint outport) { |
Charles Chan | b8caeab | 2017-06-02 19:23:51 -0700 | [diff] [blame^] | 232 | // Note: We assume that packets arrive at the edge switches have untagged VLAN. |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 233 | Ethernet ethReply = ICMP6.buildIcmp6Reply(ethRequest); |
| 234 | IPv6 icmpRequestIpv6 = (IPv6) ethRequest.getPayload(); |
| 235 | IPv6 icmpReplyIpv6 = (IPv6) ethRequest.getPayload(); |
| 236 | Ip6Address destIpAddress = Ip6Address.valueOf(icmpRequestIpv6.getSourceAddress()); |
| 237 | Ip6Address destRouterAddress = config.getRouterIpAddressForASubnetHost(destIpAddress); |
Charles Chan | b8caeab | 2017-06-02 19:23:51 -0700 | [diff] [blame^] | 238 | |
| 239 | // Note: Source IP of the ICMP request doesn't belong to any configured subnet. |
| 240 | // The source might be an indirect host behind a router. |
| 241 | // Lookup the route store for the nexthop instead. |
| 242 | if (destRouterAddress == null) { |
| 243 | Optional<ResolvedRoute> nexthop = srManager.routeService.longestPrefixLookup(destIpAddress); |
| 244 | if (nexthop.isPresent()) { |
| 245 | try { |
| 246 | destRouterAddress = config.getRouterIpv6(nexthop.get().location().deviceId()); |
| 247 | } catch (DeviceConfigNotFoundException e) { |
| 248 | log.warn("Device config not found. Abort ICMPv6 processing"); |
| 249 | return; |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 254 | int sid = config.getIPv6SegmentId(destRouterAddress); |
| 255 | if (sid < 0) { |
Charles Chan | b8caeab | 2017-06-02 19:23:51 -0700 | [diff] [blame^] | 256 | log.warn("Failed to lookup SID of the switch that {} attaches to. " + |
| 257 | "Unable to process ICMPv6 request.", destIpAddress); |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 258 | return; |
| 259 | } |
| 260 | sendPacketOut(outport, ethReply, sid, destIpAddress, icmpReplyIpv6.getHopLimit()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 261 | } |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 262 | |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 263 | /////////////////////////////////////////// |
| 264 | // ICMPv6 Neighbour Discovery Protocol // |
| 265 | /////////////////////////////////////////// |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 266 | |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 267 | /** |
| 268 | * Process incoming NDP packet. |
| 269 | * |
| 270 | * If it is an NDP request for the router or for the gateway, then sends a NDP reply. |
| 271 | * If it is an NDP request to unknown host flood in the subnet. |
| 272 | * If it is an NDP packet to known host forward the packet to the host. |
| 273 | * |
| 274 | * FIXME If the NDP packets use link local addresses we fail. |
| 275 | * |
| 276 | * @param pkt inbound packet |
| 277 | * @param hostService the host service |
| 278 | */ |
| 279 | public void processPacketIn(NeighbourMessageContext pkt, HostService hostService) { |
Charles Chan | b78b113 | 2017-03-29 17:24:39 -0700 | [diff] [blame] | 280 | // First we validate the ndp packet |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 281 | SegmentRoutingAppConfig appConfig = srManager.cfgService |
| 282 | .getConfig(srManager.appId, SegmentRoutingAppConfig.class); |
| 283 | if (appConfig != null && appConfig.suppressSubnet().contains(pkt.inPort())) { |
| 284 | // Ignore NDP packets come from suppressed ports |
| 285 | pkt.drop(); |
| 286 | return; |
| 287 | } |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 288 | |
| 289 | if (pkt.type() == NeighbourMessageType.REQUEST) { |
| 290 | handleNdpRequest(pkt, hostService); |
| 291 | } else { |
| 292 | handleNdpReply(pkt, hostService); |
| 293 | } |
| 294 | |
| 295 | } |
| 296 | |
| 297 | /** |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 298 | * Helper method to handle the ndp requests. |
| 299 | * |
| 300 | * @param pkt the ndp packet request and context information |
| 301 | * @param hostService the host service |
| 302 | */ |
| 303 | private void handleNdpRequest(NeighbourMessageContext pkt, HostService hostService) { |
Charles Chan | b3016ed | 2017-02-27 15:50:43 -0800 | [diff] [blame] | 304 | // ND request for the gateway. We have to reply on behalf of the gateway. |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 305 | if (isNdpForGateway(pkt)) { |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 306 | log.trace("Sending NDP reply on behalf of gateway IP for pkt: {}", pkt); |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 307 | sendResponse(pkt, config.getRouterMacForAGatewayIp(pkt.target()), hostService); |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 308 | } else { |
Charles Chan | b3016ed | 2017-02-27 15:50:43 -0800 | [diff] [blame] | 309 | // NOTE: Ignore NDP packets except those target for the router |
| 310 | // We will reconsider enabling this when we have host learning support |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 311 | /* |
Charles Chan | b3016ed | 2017-02-27 15:50:43 -0800 | [diff] [blame] | 312 | // ND request for an host. We do a search by Ip. |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 313 | Set<Host> hosts = hostService.getHostsByIp(pkt.target()); |
Charles Chan | b3016ed | 2017-02-27 15:50:43 -0800 | [diff] [blame] | 314 | // Possible misconfiguration ? In future this case |
| 315 | // should be handled we can have same hosts in different VLANs. |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 316 | if (hosts.size() > 1) { |
| 317 | log.warn("More than one host with IP {}", pkt.target()); |
| 318 | } |
| 319 | Host targetHost = hosts.stream().findFirst().orElse(null); |
Charles Chan | b3016ed | 2017-02-27 15:50:43 -0800 | [diff] [blame] | 320 | // If we know the host forward to its attachment point. |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 321 | if (targetHost != null) { |
| 322 | log.debug("Forward NDP request to the target host"); |
| 323 | pkt.forward(targetHost.location()); |
| 324 | } else { |
Charles Chan | b3016ed | 2017-02-27 15:50:43 -0800 | [diff] [blame] | 325 | // Flood otherwise. |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 326 | log.debug("Flood NDP request to the target subnet"); |
| 327 | flood(pkt); |
| 328 | } |
Charles Chan | b3016ed | 2017-02-27 15:50:43 -0800 | [diff] [blame] | 329 | */ |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Helper method to handle the ndp replies. |
| 335 | * |
| 336 | * @param pkt the ndp packet reply and context information |
| 337 | * @param hostService the host service |
| 338 | */ |
| 339 | private void handleNdpReply(NeighbourMessageContext pkt, HostService hostService) { |
| 340 | if (isNdpForGateway(pkt)) { |
| 341 | log.debug("Forwarding all the ip packets we stored"); |
| 342 | Ip6Address hostIpAddress = pkt.sender().getIp6Address(); |
| 343 | srManager.ipHandler.forwardPackets(pkt.inPort().deviceId(), hostIpAddress); |
| 344 | } else { |
Charles Chan | b3016ed | 2017-02-27 15:50:43 -0800 | [diff] [blame] | 345 | // NOTE: Ignore NDP packets except those target for the router |
| 346 | // We will reconsider enabling this when we have host learning support |
| 347 | /* |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 348 | HostId hostId = HostId.hostId(pkt.dstMac(), pkt.vlan()); |
| 349 | Host targetHost = hostService.getHost(hostId); |
| 350 | if (targetHost != null) { |
| 351 | log.debug("Forwarding the reply to the host"); |
| 352 | pkt.forward(targetHost.location()); |
| 353 | } else { |
Charles Chan | b3016ed | 2017-02-27 15:50:43 -0800 | [diff] [blame] | 354 | // We don't have to flood towards spine facing ports. |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 355 | if (pkt.vlan().equals(SegmentRoutingManager.INTERNAL_VLAN)) { |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 356 | return; |
| 357 | } |
| 358 | log.debug("Flooding the reply to the subnet"); |
| 359 | flood(pkt); |
| 360 | } |
Charles Chan | b3016ed | 2017-02-27 15:50:43 -0800 | [diff] [blame] | 361 | */ |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Utility to verify if the ND are for the gateway. |
| 367 | * |
| 368 | * @param pkt the ndp packet |
| 369 | * @return true if the ndp is for the gateway. False otherwise |
| 370 | */ |
| 371 | private boolean isNdpForGateway(NeighbourMessageContext pkt) { |
| 372 | DeviceId deviceId = pkt.inPort().deviceId(); |
| 373 | Set<IpAddress> gatewayIpAddresses = null; |
| 374 | try { |
| 375 | if (pkt.target().equals(config.getRouterIpv6(deviceId))) { |
| 376 | return true; |
| 377 | } |
| 378 | gatewayIpAddresses = config.getPortIPs(deviceId); |
| 379 | } catch (DeviceConfigNotFoundException e) { |
| 380 | log.warn(e.getMessage() + " Aborting check for router IP in processing ndp"); |
| 381 | } |
Charles Chan | d3727b7 | 2017-03-13 13:10:30 -0700 | [diff] [blame] | 382 | |
| 383 | return gatewayIpAddresses != null && gatewayIpAddresses.stream() |
| 384 | .filter(IpAddress::isIp6) |
| 385 | .anyMatch(gatewayIp -> gatewayIp.equals(pkt.target()) || |
| 386 | Arrays.equals(IPv6.getSolicitNodeAddress(gatewayIp.toOctets()), |
| 387 | pkt.target().toOctets()) |
| 388 | ); |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /** |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 392 | * Sends a NDP request for the target IP address to all ports except in-port. |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 393 | * |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 394 | * @param deviceId Switch device ID |
| 395 | * @param targetAddress target IP address for ARP |
| 396 | * @param inPort in-port |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 397 | */ |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 398 | public void sendNdpRequest(DeviceId deviceId, IpAddress targetAddress, ConnectPoint inPort) { |
| 399 | byte[] senderMacAddress = new byte[MacAddress.MAC_ADDRESS_LENGTH]; |
| 400 | byte[] senderIpAddress = new byte[Ip6Address.BYTE_LENGTH]; |
Charles Chan | b78b113 | 2017-03-29 17:24:39 -0700 | [diff] [blame] | 401 | // Retrieves device info. |
Pier Luigi | 6a83c4a | 2017-01-29 12:38:48 -0800 | [diff] [blame] | 402 | if (!getSenderInfo(senderMacAddress, senderIpAddress, deviceId, targetAddress)) { |
| 403 | log.warn("Aborting sendNdpRequest, we cannot get all the information needed"); |
| 404 | return; |
| 405 | } |
Charles Chan | b78b113 | 2017-03-29 17:24:39 -0700 | [diff] [blame] | 406 | // We have to compute the dst mac address and dst ip address. |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 407 | byte[] dstIp = IPv6.getSolicitNodeAddress(targetAddress.toOctets()); |
| 408 | byte[] dstMac = IPv6.getMCastMacAddress(dstIp); |
Charles Chan | b78b113 | 2017-03-29 17:24:39 -0700 | [diff] [blame] | 409 | // Creates the request. |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 410 | Ethernet ndpRequest = NeighborSolicitation.buildNdpSolicit( |
| 411 | targetAddress.toOctets(), |
| 412 | senderIpAddress, |
| 413 | dstIp, |
| 414 | senderMacAddress, |
| 415 | dstMac, |
| 416 | VlanId.NONE |
| 417 | ); |
| 418 | flood(ndpRequest, inPort, targetAddress); |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 419 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 420 | } |