sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
sangho | b35a619 | 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 | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 18 | import org.onlab.packet.Ethernet; |
| 19 | import org.onlab.packet.ICMP; |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 20 | import org.onlab.packet.ICMP6; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 21 | import org.onlab.packet.IPv4; |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 22 | import org.onlab.packet.IPv6; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 23 | import org.onlab.packet.Ip4Address; |
Pier Ventre | f4b5fce | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 24 | import org.onlab.packet.Ip6Address; |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 25 | import org.onlab.packet.IpAddress; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 26 | import org.onlab.packet.MPLS; |
Pier Ventre | f4b5fce | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 27 | import org.onlab.packet.MacAddress; |
| 28 | import org.onlab.packet.VlanId; |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 29 | import org.onlab.packet.ndp.NeighborSolicitation; |
Ray Milkey | b65d784 | 2017-08-03 16:28:24 -0700 | [diff] [blame] | 30 | import org.onosproject.net.neighbour.NeighbourMessageContext; |
| 31 | import org.onosproject.net.neighbour.NeighbourMessageType; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 32 | import org.onosproject.net.ConnectPoint; |
| 33 | import org.onosproject.net.DeviceId; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame^] | 34 | import org.onosproject.net.intf.Interface; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 35 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 36 | import org.onosproject.net.flow.TrafficTreatment; |
Pier Ventre | f4b5fce | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 37 | import org.onosproject.net.host.HostService; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 38 | import org.onosproject.net.packet.DefaultOutboundPacket; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 39 | import org.onosproject.net.packet.OutboundPacket; |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 40 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
Pier Ventre | f4b5fce | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 41 | import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 42 | import org.slf4j.Logger; |
| 43 | import org.slf4j.LoggerFactory; |
| 44 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 45 | import java.nio.ByteBuffer; |
Charles Chan | 0ed44fb | 2017-03-13 13:10:30 -0700 | [diff] [blame] | 46 | import java.util.Arrays; |
Charles Chan | 40abef4 | 2017-06-02 19:23:51 -0700 | [diff] [blame] | 47 | import java.util.Optional; |
Saurav Das | 837e0bb | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 48 | import java.util.Set; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 49 | |
Charles Chan | e849c19 | 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 | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 54 | public class IcmpHandler extends SegmentRoutingNeighbourHandler { |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 55 | |
| 56 | private static Logger log = LoggerFactory.getLogger(IcmpHandler.class); |
sangho | b35a619 | 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 | 735b8c8 | 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 { |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 96 | log.trace("Send a MPLS packet as a ICMP response"); |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 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 | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Pier Ventre | f4b5fce | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 115 | ////////////////////////////////////// |
| 116 | // ICMP Echo/Reply Protocol // |
| 117 | ////////////////////////////////////// |
| 118 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 119 | /** |
| 120 | * Process incoming ICMP packet. |
Charles Chan | d4f3a62 | 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 | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 123 | * |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 124 | * @param eth inbound ICMP packet |
| 125 | * @param inPort the input port |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 126 | */ |
Pier Ventre | 735b8c8 | 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 | 10bd8d1 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 131 | Set<IpAddress> gatewayIpAddresses = config.getPortIPs(deviceId); |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 132 | IpAddress routerIp; |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 133 | try { |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 134 | routerIp = config.getRouterIpv4(deviceId); |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 135 | } catch (DeviceConfigNotFoundException e) { |
| 136 | log.warn(e.getMessage() + " Aborting processPacketIn."); |
| 137 | return; |
| 138 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 139 | // ICMP to the router IP or gateway IP |
Pier Ventre | 735b8c8 | 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 | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 142 | gatewayIpAddresses.contains(destinationAddress))) { |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 143 | sendIcmpResponse(eth, inPort); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 144 | } else { |
Charles Chan | d4f3a62 | 2017-03-29 17:24:39 -0700 | [diff] [blame] | 145 | log.trace("Ignore ICMP that targets for {}", destinationAddress); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 146 | } |
Charles Chan | d4f3a62 | 2017-03-29 17:24:39 -0700 | [diff] [blame] | 147 | // We remove the packet from the queue |
| 148 | srManager.ipHandler.dequeuePacket(ipv4Packet, destinationAddress); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 151 | /** |
| 152 | * Sends an ICMP reply message. |
| 153 | * |
Charles Chan | 68aa62d | 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 | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 157 | private void sendIcmpResponse(Ethernet icmpRequest, ConnectPoint outport) { |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 158 | Ethernet icmpReplyEth = ICMP.buildIcmpReply(icmpRequest); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 159 | IPv4 icmpRequestIpv4 = (IPv4) icmpRequest.getPayload(); |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 160 | IPv4 icmpReplyIpv4 = (IPv4) icmpReplyEth.getPayload(); |
| 161 | Ip4Address destIpAddress = Ip4Address.valueOf(icmpRequestIpv4.getSourceAddress()); |
sangho | 666cd6d | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 162 | Ip4Address destRouterAddress = config.getRouterIpAddressForASubnetHost(destIpAddress); |
Charles Chan | 40abef4 | 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) { |
Charles Chan | 9640c81 | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 168 | Optional<DeviceId> deviceId = srManager.routeService |
| 169 | .longestPrefixLookup(destIpAddress).map(srManager::nextHopLocations) |
| 170 | .flatMap(locations -> locations.stream().findFirst()) |
| 171 | .map(ConnectPoint::deviceId); |
| 172 | if (deviceId.isPresent()) { |
Charles Chan | 40abef4 | 2017-06-02 19:23:51 -0700 | [diff] [blame] | 173 | try { |
Charles Chan | 9640c81 | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 174 | destRouterAddress = config.getRouterIpv4(deviceId.get()); |
Charles Chan | 40abef4 | 2017-06-02 19:23:51 -0700 | [diff] [blame] | 175 | } catch (DeviceConfigNotFoundException e) { |
Charles Chan | 9640c81 | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 176 | log.warn("Device config for {} not found. Abort ICMP processing", deviceId); |
Charles Chan | 40abef4 | 2017-06-02 19:23:51 -0700 | [diff] [blame] | 177 | return; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 182 | int destSid = config.getIPv4SegmentId(destRouterAddress); |
Charles Chan | 68aaad5 | 2016-12-09 12:54:49 -0800 | [diff] [blame] | 183 | if (destSid < 0) { |
Charles Chan | 40abef4 | 2017-06-02 19:23:51 -0700 | [diff] [blame] | 184 | log.warn("Failed to lookup SID of the switch that {} attaches to. " + |
| 185 | "Unable to process ICMP request.", destIpAddress); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 186 | return; |
| 187 | } |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 188 | sendPacketOut(outport, icmpReplyEth, destSid, destIpAddress, icmpReplyIpv4.getTtl()); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 191 | /////////////////////////////////////////// |
| 192 | // ICMPv6 Echo/Reply Protocol // |
| 193 | /////////////////////////////////////////// |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 194 | |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 195 | /** |
| 196 | * Process incoming ICMPv6 packet. |
Charles Chan | d4f3a62 | 2017-03-29 17:24:39 -0700 | [diff] [blame] | 197 | * If it is an ICMPv6 request to router, then sends an ICMPv6 response. |
| 198 | * Otherwise ignore the packet. |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 199 | * |
| 200 | * @param eth the incoming ICMPv6 packet |
| 201 | * @param inPort the input port |
| 202 | */ |
| 203 | public void processIcmpv6(Ethernet eth, ConnectPoint inPort) { |
| 204 | DeviceId deviceId = inPort.deviceId(); |
| 205 | IPv6 ipv6Packet = (IPv6) eth.getPayload(); |
| 206 | Ip6Address destinationAddress = Ip6Address.valueOf(ipv6Packet.getDestinationAddress()); |
| 207 | Set<IpAddress> gatewayIpAddresses = config.getPortIPs(deviceId); |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame^] | 208 | MacAddress interfaceMac; |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 209 | IpAddress routerIp; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame^] | 210 | |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 211 | try { |
| 212 | routerIp = config.getRouterIpv6(deviceId); |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame^] | 213 | Optional<MacAddress> macAddress = srManager.interfaceService.getInterfacesByPort(inPort).stream() |
| 214 | .map(Interface::mac) |
| 215 | .findFirst(); |
| 216 | if (!macAddress.isPresent()) { |
| 217 | log.warn("Failed in fetching MAC address of {}. Aborting ICMP6 processing.", inPort); |
| 218 | return; |
| 219 | } |
| 220 | interfaceMac = MacAddress.valueOf(macAddress.get().toBytes()); |
| 221 | |
| 222 | // Ensure ICMP to the router IP, gateway IP or link-local EUI-64 |
| 223 | ICMP6 icmp6 = (ICMP6) ipv6Packet.getPayload(); |
| 224 | if (icmp6.getIcmpType() == ICMP6.ECHO_REQUEST && (destinationAddress.equals(routerIp.getIp6Address()) || |
| 225 | destinationAddress.equals(Ip6Address.valueOf(IPv6.getLinkLocalAddress(interfaceMac.toBytes()))) || |
| 226 | gatewayIpAddresses.contains(destinationAddress))) { |
| 227 | sendIcmpv6Response(eth, inPort); |
| 228 | } else { |
| 229 | log.trace("Ignore ICMPv6 that targets for {}", destinationAddress); |
| 230 | } |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 231 | } catch (DeviceConfigNotFoundException e) { |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame^] | 232 | log.warn(e.getMessage() + " Ignore ICMPv6 that targets to {}.", destinationAddress); |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Sends an ICMPv6 reply message. |
| 238 | * |
| 239 | * Note: we assume that packets sending from the edge switches to the hosts |
| 240 | * have untagged VLAN. |
| 241 | * @param ethRequest the original ICMP request |
| 242 | * @param outport the output port where the ICMP reply should be sent to |
| 243 | */ |
| 244 | private void sendIcmpv6Response(Ethernet ethRequest, ConnectPoint outport) { |
Charles Chan | 40abef4 | 2017-06-02 19:23:51 -0700 | [diff] [blame] | 245 | // Note: We assume that packets arrive at the edge switches have untagged VLAN. |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 246 | Ethernet ethReply = ICMP6.buildIcmp6Reply(ethRequest); |
| 247 | IPv6 icmpRequestIpv6 = (IPv6) ethRequest.getPayload(); |
| 248 | IPv6 icmpReplyIpv6 = (IPv6) ethRequest.getPayload(); |
| 249 | Ip6Address destIpAddress = Ip6Address.valueOf(icmpRequestIpv6.getSourceAddress()); |
| 250 | Ip6Address destRouterAddress = config.getRouterIpAddressForASubnetHost(destIpAddress); |
Charles Chan | 40abef4 | 2017-06-02 19:23:51 -0700 | [diff] [blame] | 251 | |
| 252 | // Note: Source IP of the ICMP request doesn't belong to any configured subnet. |
| 253 | // The source might be an indirect host behind a router. |
| 254 | // Lookup the route store for the nexthop instead. |
| 255 | if (destRouterAddress == null) { |
Charles Chan | 9640c81 | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 256 | Optional<DeviceId> deviceId = srManager.routeService |
| 257 | .longestPrefixLookup(destIpAddress).map(srManager::nextHopLocations) |
| 258 | .flatMap(locations -> locations.stream().findFirst()) |
| 259 | .map(ConnectPoint::deviceId); |
| 260 | if (deviceId.isPresent()) { |
Charles Chan | 40abef4 | 2017-06-02 19:23:51 -0700 | [diff] [blame] | 261 | try { |
Charles Chan | 9640c81 | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 262 | destRouterAddress = config.getRouterIpv6(deviceId.get()); |
Charles Chan | 40abef4 | 2017-06-02 19:23:51 -0700 | [diff] [blame] | 263 | } catch (DeviceConfigNotFoundException e) { |
Charles Chan | 9640c81 | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 264 | log.warn("Device config for {} not found. Abort ICMPv6 processing", deviceId); |
Charles Chan | 40abef4 | 2017-06-02 19:23:51 -0700 | [diff] [blame] | 265 | return; |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame^] | 270 | // Search SID only if store lookup is success otherwise proceed with "sid=-1" |
| 271 | int sid = -1; |
| 272 | if (destRouterAddress != null) { |
| 273 | sid = config.getIPv6SegmentId(destRouterAddress); |
| 274 | if (sid < 0) { |
| 275 | log.warn("Failed to lookup SID of the switch that {} attaches to. " + |
| 276 | "Unable to process ICMPv6 request.", destIpAddress); |
| 277 | return; |
| 278 | } |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 279 | } |
| 280 | sendPacketOut(outport, ethReply, sid, destIpAddress, icmpReplyIpv6.getHopLimit()); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 281 | } |
sangho | 666cd6d | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 282 | |
Pier Ventre | f4b5fce | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 283 | /////////////////////////////////////////// |
| 284 | // ICMPv6 Neighbour Discovery Protocol // |
| 285 | /////////////////////////////////////////// |
sangho | 666cd6d | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 286 | |
Pier Ventre | f4b5fce | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 287 | /** |
| 288 | * Process incoming NDP packet. |
| 289 | * |
| 290 | * If it is an NDP request for the router or for the gateway, then sends a NDP reply. |
| 291 | * If it is an NDP request to unknown host flood in the subnet. |
| 292 | * If it is an NDP packet to known host forward the packet to the host. |
| 293 | * |
| 294 | * FIXME If the NDP packets use link local addresses we fail. |
| 295 | * |
| 296 | * @param pkt inbound packet |
| 297 | * @param hostService the host service |
| 298 | */ |
| 299 | public void processPacketIn(NeighbourMessageContext pkt, HostService hostService) { |
Charles Chan | d4f3a62 | 2017-03-29 17:24:39 -0700 | [diff] [blame] | 300 | // First we validate the ndp packet |
Pier Ventre | f4b5fce | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 301 | SegmentRoutingAppConfig appConfig = srManager.cfgService |
| 302 | .getConfig(srManager.appId, SegmentRoutingAppConfig.class); |
| 303 | if (appConfig != null && appConfig.suppressSubnet().contains(pkt.inPort())) { |
| 304 | // Ignore NDP packets come from suppressed ports |
| 305 | pkt.drop(); |
| 306 | return; |
| 307 | } |
Pier Ventre | f4b5fce | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 308 | |
| 309 | if (pkt.type() == NeighbourMessageType.REQUEST) { |
| 310 | handleNdpRequest(pkt, hostService); |
| 311 | } else { |
| 312 | handleNdpReply(pkt, hostService); |
| 313 | } |
| 314 | |
| 315 | } |
| 316 | |
| 317 | /** |
Pier Ventre | f4b5fce | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 318 | * Helper method to handle the ndp requests. |
Pier Ventre | f4b5fce | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 319 | * @param pkt the ndp packet request and context information |
| 320 | * @param hostService the host service |
| 321 | */ |
| 322 | private void handleNdpRequest(NeighbourMessageContext pkt, HostService hostService) { |
Charles Chan | 563a781 | 2017-02-27 15:50:43 -0800 | [diff] [blame] | 323 | // ND request for the gateway. We have to reply on behalf of the gateway. |
Pier Ventre | f4b5fce | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 324 | if (isNdpForGateway(pkt)) { |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame^] | 325 | log.trace("Sending NDP reply on behalf of gateway IP for pkt: {}", pkt.target()); |
| 326 | MacAddress routerMac = config.getRouterMacForAGatewayIp(pkt.target()); |
| 327 | sendResponse(pkt, routerMac, hostService); |
Pier Ventre | f4b5fce | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 328 | } else { |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame^] | 329 | |
| 330 | // Process NDP targets towards EUI-64 address. |
| 331 | try { |
| 332 | DeviceId deviceId = pkt.inPort().deviceId(); |
| 333 | Optional<MacAddress> macAddress = srManager.interfaceService.getInterfacesByPort(pkt.inPort()) |
| 334 | .stream() |
| 335 | .map(Interface::mac) |
| 336 | .findFirst(); |
| 337 | if (!macAddress.isPresent()) { |
| 338 | log.warn("Failed in fetching MAC address of {}. Aborting NDP processing.", pkt.inPort()); |
| 339 | return; |
| 340 | } |
| 341 | MacAddress interfaceMac = MacAddress.valueOf(macAddress.get().toBytes()); |
| 342 | Ip6Address interfaceLinkLocalIP = Ip6Address.valueOf(IPv6.getLinkLocalAddress(interfaceMac.toBytes())); |
| 343 | if (pkt.target().equals(interfaceLinkLocalIP)) { |
| 344 | MacAddress routerMac = config.getDeviceMac(deviceId); |
| 345 | sendResponse(pkt, routerMac, hostService); |
| 346 | } |
| 347 | } catch (DeviceConfigNotFoundException e) { |
| 348 | log.warn(e.getMessage() + " Unable to handle NDP packet to {}. Aborting.", pkt.target()); |
| 349 | return; |
| 350 | } |
| 351 | |
Charles Chan | 563a781 | 2017-02-27 15:50:43 -0800 | [diff] [blame] | 352 | // NOTE: Ignore NDP packets except those target for the router |
| 353 | // We will reconsider enabling this when we have host learning support |
Pier Ventre | f4b5fce | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 354 | /* |
Charles Chan | 563a781 | 2017-02-27 15:50:43 -0800 | [diff] [blame] | 355 | // ND request for an host. We do a search by Ip. |
Pier Ventre | f4b5fce | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 356 | Set<Host> hosts = hostService.getHostsByIp(pkt.target()); |
Charles Chan | 563a781 | 2017-02-27 15:50:43 -0800 | [diff] [blame] | 357 | // Possible misconfiguration ? In future this case |
| 358 | // should be handled we can have same hosts in different VLANs. |
Pier Ventre | f4b5fce | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 359 | if (hosts.size() > 1) { |
| 360 | log.warn("More than one host with IP {}", pkt.target()); |
| 361 | } |
| 362 | Host targetHost = hosts.stream().findFirst().orElse(null); |
Charles Chan | 563a781 | 2017-02-27 15:50:43 -0800 | [diff] [blame] | 363 | // If we know the host forward to its attachment point. |
Pier Ventre | f4b5fce | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 364 | if (targetHost != null) { |
| 365 | log.debug("Forward NDP request to the target host"); |
| 366 | pkt.forward(targetHost.location()); |
| 367 | } else { |
Charles Chan | 563a781 | 2017-02-27 15:50:43 -0800 | [diff] [blame] | 368 | // Flood otherwise. |
Pier Ventre | f4b5fce | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 369 | log.debug("Flood NDP request to the target subnet"); |
| 370 | flood(pkt); |
| 371 | } |
Charles Chan | 563a781 | 2017-02-27 15:50:43 -0800 | [diff] [blame] | 372 | */ |
Pier Ventre | f4b5fce | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Helper method to handle the ndp replies. |
| 378 | * |
| 379 | * @param pkt the ndp packet reply and context information |
| 380 | * @param hostService the host service |
| 381 | */ |
| 382 | private void handleNdpReply(NeighbourMessageContext pkt, HostService hostService) { |
| 383 | if (isNdpForGateway(pkt)) { |
| 384 | log.debug("Forwarding all the ip packets we stored"); |
| 385 | Ip6Address hostIpAddress = pkt.sender().getIp6Address(); |
| 386 | srManager.ipHandler.forwardPackets(pkt.inPort().deviceId(), hostIpAddress); |
| 387 | } else { |
Charles Chan | 563a781 | 2017-02-27 15:50:43 -0800 | [diff] [blame] | 388 | // NOTE: Ignore NDP packets except those target for the router |
| 389 | // We will reconsider enabling this when we have host learning support |
| 390 | /* |
Pier Ventre | f4b5fce | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 391 | HostId hostId = HostId.hostId(pkt.dstMac(), pkt.vlan()); |
| 392 | Host targetHost = hostService.getHost(hostId); |
| 393 | if (targetHost != null) { |
| 394 | log.debug("Forwarding the reply to the host"); |
| 395 | pkt.forward(targetHost.location()); |
| 396 | } else { |
Charles Chan | 563a781 | 2017-02-27 15:50:43 -0800 | [diff] [blame] | 397 | // We don't have to flood towards spine facing ports. |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 398 | if (pkt.vlan().equals(SegmentRoutingManager.INTERNAL_VLAN)) { |
Pier Ventre | f4b5fce | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 399 | return; |
| 400 | } |
| 401 | log.debug("Flooding the reply to the subnet"); |
| 402 | flood(pkt); |
| 403 | } |
Charles Chan | 563a781 | 2017-02-27 15:50:43 -0800 | [diff] [blame] | 404 | */ |
Pier Ventre | f4b5fce | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Utility to verify if the ND are for the gateway. |
| 410 | * |
| 411 | * @param pkt the ndp packet |
| 412 | * @return true if the ndp is for the gateway. False otherwise |
| 413 | */ |
| 414 | private boolean isNdpForGateway(NeighbourMessageContext pkt) { |
| 415 | DeviceId deviceId = pkt.inPort().deviceId(); |
| 416 | Set<IpAddress> gatewayIpAddresses = null; |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame^] | 417 | |
Pier Ventre | f4b5fce | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 418 | try { |
| 419 | if (pkt.target().equals(config.getRouterIpv6(deviceId))) { |
| 420 | return true; |
| 421 | } |
| 422 | gatewayIpAddresses = config.getPortIPs(deviceId); |
| 423 | } catch (DeviceConfigNotFoundException e) { |
| 424 | log.warn(e.getMessage() + " Aborting check for router IP in processing ndp"); |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame^] | 425 | return false; |
Pier Ventre | f4b5fce | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 426 | } |
Charles Chan | 0ed44fb | 2017-03-13 13:10:30 -0700 | [diff] [blame] | 427 | return gatewayIpAddresses != null && gatewayIpAddresses.stream() |
| 428 | .filter(IpAddress::isIp6) |
| 429 | .anyMatch(gatewayIp -> gatewayIp.equals(pkt.target()) || |
| 430 | Arrays.equals(IPv6.getSolicitNodeAddress(gatewayIp.toOctets()), |
Jayakumar Thazhath | 3ec6aa2 | 2017-09-11 02:00:20 -0400 | [diff] [blame^] | 431 | pkt.target().toOctets())); |
Pier Ventre | f4b5fce | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /** |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 435 | * Sends a NDP request for the target IP address to all ports except in-port. |
Pier Ventre | f4b5fce | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 436 | * |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 437 | * @param deviceId Switch device ID |
| 438 | * @param targetAddress target IP address for ARP |
| 439 | * @param inPort in-port |
Pier Ventre | f4b5fce | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 440 | */ |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 441 | public void sendNdpRequest(DeviceId deviceId, IpAddress targetAddress, ConnectPoint inPort) { |
| 442 | byte[] senderMacAddress = new byte[MacAddress.MAC_ADDRESS_LENGTH]; |
| 443 | byte[] senderIpAddress = new byte[Ip6Address.BYTE_LENGTH]; |
Charles Chan | d4f3a62 | 2017-03-29 17:24:39 -0700 | [diff] [blame] | 444 | // Retrieves device info. |
Pier Luigi | a905c0c | 2017-01-29 12:38:48 -0800 | [diff] [blame] | 445 | if (!getSenderInfo(senderMacAddress, senderIpAddress, deviceId, targetAddress)) { |
| 446 | log.warn("Aborting sendNdpRequest, we cannot get all the information needed"); |
| 447 | return; |
| 448 | } |
Charles Chan | d4f3a62 | 2017-03-29 17:24:39 -0700 | [diff] [blame] | 449 | // We have to compute the dst mac address and dst ip address. |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 450 | byte[] dstIp = IPv6.getSolicitNodeAddress(targetAddress.toOctets()); |
| 451 | byte[] dstMac = IPv6.getMCastMacAddress(dstIp); |
Charles Chan | d4f3a62 | 2017-03-29 17:24:39 -0700 | [diff] [blame] | 452 | // Creates the request. |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 453 | Ethernet ndpRequest = NeighborSolicitation.buildNdpSolicit( |
| 454 | targetAddress.toOctets(), |
| 455 | senderIpAddress, |
| 456 | dstIp, |
| 457 | senderMacAddress, |
| 458 | dstMac, |
| 459 | VlanId.NONE |
| 460 | ); |
| 461 | flood(ndpRequest, inPort, targetAddress); |
Pier Ventre | f4b5fce | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 462 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 463 | } |