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.IpPrefix; |
| 27 | import org.onlab.packet.MPLS; |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 28 | import org.onlab.packet.MacAddress; |
| 29 | import org.onlab.packet.VlanId; |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 30 | import org.onlab.packet.ndp.NeighborSolicitation; |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 31 | import org.onosproject.incubator.net.neighbour.NeighbourMessageContext; |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 32 | import org.onosproject.incubator.net.neighbour.NeighbourMessageType; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 33 | import org.onosproject.net.ConnectPoint; |
| 34 | import org.onosproject.net.DeviceId; |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 35 | import org.onosproject.net.Host; |
| 36 | import org.onosproject.net.HostId; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 37 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 38 | import org.onosproject.net.flow.TrafficTreatment; |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 39 | import org.onosproject.net.host.HostService; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 40 | import org.onosproject.net.packet.DefaultOutboundPacket; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 41 | import org.onosproject.net.packet.OutboundPacket; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 42 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 43 | import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 44 | import org.slf4j.Logger; |
| 45 | import org.slf4j.LoggerFactory; |
| 46 | |
Jonathan Hart | d53ebc4 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 47 | import java.nio.ByteBuffer; |
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. |
| 121 | * If it is an ICMP request to router or known host, then sends an ICMP response. |
| 122 | * If it is an ICMP packet to known host and forward the packet to the host. |
| 123 | * If it is an ICMP packet to unknown host in a subnet, then sends an ARP request |
| 124 | * to the subnet. |
| 125 | * |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 126 | * @param eth inbound ICMP packet |
| 127 | * @param inPort the input port |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 128 | */ |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 129 | public void processIcmp(Ethernet eth, ConnectPoint inPort) { |
| 130 | DeviceId deviceId = inPort.deviceId(); |
| 131 | IPv4 ipv4Packet = (IPv4) eth.getPayload(); |
| 132 | Ip4Address destinationAddress = Ip4Address.valueOf(ipv4Packet.getDestinationAddress()); |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 133 | Set<IpAddress> gatewayIpAddresses = config.getPortIPs(deviceId); |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 134 | IpAddress routerIp; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 135 | try { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 136 | routerIp = config.getRouterIpv4(deviceId); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 137 | } catch (DeviceConfigNotFoundException e) { |
| 138 | log.warn(e.getMessage() + " Aborting processPacketIn."); |
| 139 | return; |
| 140 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 141 | // ICMP to the router IP or gateway IP |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 142 | if (((ICMP) ipv4Packet.getPayload()).getIcmpType() == ICMP.TYPE_ECHO_REQUEST && |
| 143 | (destinationAddress.equals(routerIp.getIp4Address()) || |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 144 | gatewayIpAddresses.contains(destinationAddress))) { |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 145 | sendIcmpResponse(eth, inPort); |
| 146 | // We remove the packet from the queue |
| 147 | srManager.ipHandler.dequeuePacket(ipv4Packet, destinationAddress); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 148 | |
| 149 | // ICMP for any known host |
| 150 | } else if (!srManager.hostService.getHostsByIp(destinationAddress).isEmpty()) { |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 151 | // TODO: known host packet should not be coming to controller - resend flows? |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 152 | srManager.ipHandler.forwardPackets(deviceId, destinationAddress); |
| 153 | |
| 154 | // ICMP for an unknown host in the subnet of the router |
| 155 | } else if (config.inSameSubnet(deviceId, destinationAddress)) { |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 156 | srManager.arpHandler.sendArpRequest(deviceId, destinationAddress, inPort); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 157 | |
| 158 | // ICMP for an unknown host |
| 159 | } else { |
| 160 | log.debug("ICMP request for unknown host {} ", destinationAddress); |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 161 | // We remove the packet from the queue |
| 162 | srManager.ipHandler.dequeuePacket(ipv4Packet, destinationAddress); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 166 | /** |
| 167 | * Sends an ICMP reply message. |
| 168 | * |
| 169 | * Note: we assume that packets sending from the edge switches to the hosts |
| 170 | * have untagged VLAN. |
| 171 | * @param icmpRequest the original ICMP request |
| 172 | * @param outport the output port where the ICMP reply should be sent to |
| 173 | */ |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 174 | private void sendIcmpResponse(Ethernet icmpRequest, ConnectPoint outport) { |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 175 | // Note: We assume that packets arrive at the edge switches have |
| 176 | // untagged VLAN. |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 177 | Ethernet icmpReplyEth = ICMP.buildIcmpReply(icmpRequest); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 178 | IPv4 icmpRequestIpv4 = (IPv4) icmpRequest.getPayload(); |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 179 | IPv4 icmpReplyIpv4 = (IPv4) icmpReplyEth.getPayload(); |
| 180 | Ip4Address destIpAddress = Ip4Address.valueOf(icmpRequestIpv4.getSourceAddress()); |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 181 | Ip4Address destRouterAddress = config.getRouterIpAddressForASubnetHost(destIpAddress); |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 182 | int destSid = config.getIPv4SegmentId(destRouterAddress); |
Charles Chan | 7066136 | 2016-12-09 12:54:49 -0800 | [diff] [blame] | 183 | if (destSid < 0) { |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 184 | log.warn("Cannot find the Segment ID for {}", destIpAddress); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 185 | return; |
| 186 | } |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 187 | sendPacketOut(outport, icmpReplyEth, destSid, destIpAddress, icmpReplyIpv4.getTtl()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 190 | /////////////////////////////////////////// |
| 191 | // ICMPv6 Echo/Reply Protocol // |
| 192 | /////////////////////////////////////////// |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 193 | |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 194 | /** |
| 195 | * Process incoming ICMPv6 packet. |
| 196 | * If it is an ICMP request to router or known host, then sends an ICMP response. |
| 197 | * If it is an ICMP packet to known host and forward the packet to the host. |
| 198 | * If it is an ICMP packet to unknown host in a subnet, then sends an ARP request |
| 199 | * to the subnet. |
| 200 | * |
| 201 | * @param eth the incoming ICMPv6 packet |
| 202 | * @param inPort the input port |
| 203 | */ |
| 204 | public void processIcmpv6(Ethernet eth, ConnectPoint inPort) { |
| 205 | DeviceId deviceId = inPort.deviceId(); |
| 206 | IPv6 ipv6Packet = (IPv6) eth.getPayload(); |
| 207 | Ip6Address destinationAddress = Ip6Address.valueOf(ipv6Packet.getDestinationAddress()); |
| 208 | Set<IpAddress> gatewayIpAddresses = config.getPortIPs(deviceId); |
| 209 | IpAddress routerIp; |
| 210 | try { |
| 211 | routerIp = config.getRouterIpv6(deviceId); |
| 212 | } catch (DeviceConfigNotFoundException e) { |
| 213 | log.warn(e.getMessage() + " Aborting processPacketIn."); |
| 214 | return; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 215 | } |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 216 | ICMP6 icmp6 = (ICMP6) ipv6Packet.getPayload(); |
| 217 | // ICMP to the router IP or gateway IP |
| 218 | if (icmp6.getIcmpType() == ICMP6.ECHO_REQUEST && |
| 219 | (destinationAddress.equals(routerIp.getIp6Address()) || |
| 220 | gatewayIpAddresses.contains(destinationAddress))) { |
| 221 | sendIcmpv6Response(eth, inPort); |
| 222 | // We remove the packet from the queue |
| 223 | srManager.ipHandler.dequeuePacket(ipv6Packet, destinationAddress); |
| 224 | // ICMP for any known host |
| 225 | } else if (!srManager.hostService.getHostsByIp(destinationAddress).isEmpty()) { |
| 226 | // TODO: known host packet should not be coming to controller - resend flows? |
| 227 | srManager.ipHandler.forwardPackets(deviceId, destinationAddress); |
| 228 | // ICMP for an unknown host in the subnet of the router |
| 229 | } else if (config.inSameSubnet(deviceId, destinationAddress)) { |
| 230 | sendNdpRequest(deviceId, destinationAddress, inPort); |
| 231 | // ICMP for an unknown host or not configured host |
| 232 | } else { |
| 233 | log.debug("ICMPv6 request for unknown host or not configured host {} ", destinationAddress); |
| 234 | // We remove the packet from the queue |
| 235 | srManager.ipHandler.dequeuePacket(ipv6Packet, destinationAddress); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Sends an ICMPv6 reply message. |
| 241 | * |
| 242 | * Note: we assume that packets sending from the edge switches to the hosts |
| 243 | * have untagged VLAN. |
| 244 | * @param ethRequest the original ICMP request |
| 245 | * @param outport the output port where the ICMP reply should be sent to |
| 246 | */ |
| 247 | private void sendIcmpv6Response(Ethernet ethRequest, ConnectPoint outport) { |
| 248 | // Note: We assume that packets arrive at the edge switches have |
| 249 | // untagged VLAN. |
| 250 | Ethernet ethReply = ICMP6.buildIcmp6Reply(ethRequest); |
| 251 | IPv6 icmpRequestIpv6 = (IPv6) ethRequest.getPayload(); |
| 252 | IPv6 icmpReplyIpv6 = (IPv6) ethRequest.getPayload(); |
| 253 | Ip6Address destIpAddress = Ip6Address.valueOf(icmpRequestIpv6.getSourceAddress()); |
| 254 | Ip6Address destRouterAddress = config.getRouterIpAddressForASubnetHost(destIpAddress); |
| 255 | int sid = config.getIPv6SegmentId(destRouterAddress); |
| 256 | if (sid < 0) { |
| 257 | log.warn("Cannot find the Segment ID for {}", destIpAddress); |
| 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) { |
| 280 | /* |
| 281 | * First we validate the ndp packet |
| 282 | */ |
| 283 | SegmentRoutingAppConfig appConfig = srManager.cfgService |
| 284 | .getConfig(srManager.appId, SegmentRoutingAppConfig.class); |
| 285 | if (appConfig != null && appConfig.suppressSubnet().contains(pkt.inPort())) { |
| 286 | // Ignore NDP packets come from suppressed ports |
| 287 | pkt.drop(); |
| 288 | return; |
| 289 | } |
| 290 | if (!validateSrcIp(pkt)) { |
| 291 | log.debug("Ignore NDP packet discovered on {} with unexpected src ip address {}.", |
| 292 | pkt.inPort(), pkt.sender()); |
| 293 | pkt.drop(); |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | if (pkt.type() == NeighbourMessageType.REQUEST) { |
| 298 | handleNdpRequest(pkt, hostService); |
| 299 | } else { |
| 300 | handleNdpReply(pkt, hostService); |
| 301 | } |
| 302 | |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Utility function to verify if the src ip belongs to the same |
| 307 | * subnet configured on the port it is seen. |
| 308 | * |
| 309 | * @param pkt the ndp packet and context information |
| 310 | * @return true if the src ip is a valid address for the subnet configured |
| 311 | * for the connect point |
| 312 | */ |
| 313 | private boolean validateSrcIp(NeighbourMessageContext pkt) { |
| 314 | ConnectPoint connectPoint = pkt.inPort(); |
| 315 | IpPrefix subnet = config.getPortIPv6Subnet( |
| 316 | connectPoint.deviceId(), |
| 317 | connectPoint.port() |
| 318 | ).getIp6Prefix(); |
| 319 | return subnet != null && subnet.contains(pkt.sender()); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Helper method to handle the ndp requests. |
| 324 | * |
| 325 | * @param pkt the ndp packet request and context information |
| 326 | * @param hostService the host service |
| 327 | */ |
| 328 | private void handleNdpRequest(NeighbourMessageContext pkt, HostService hostService) { |
| 329 | /* |
| 330 | * ND request for the gateway. We have to reply on behalf |
| 331 | * of the gateway. |
| 332 | */ |
| 333 | if (isNdpForGateway(pkt)) { |
| 334 | log.debug("Sending NDP reply on behalf of the router"); |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 335 | sendResponse(pkt, config.getRouterMacForAGatewayIp(pkt.target()), hostService); |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 336 | } else { |
| 337 | /* |
| 338 | * ND request for an host. We do a search by Ip. |
| 339 | */ |
| 340 | Set<Host> hosts = hostService.getHostsByIp(pkt.target()); |
| 341 | /* |
| 342 | * Possible misconfiguration ? In future this case |
| 343 | * should be handled we can have same hosts in different |
| 344 | * vlans. |
| 345 | */ |
| 346 | if (hosts.size() > 1) { |
| 347 | log.warn("More than one host with IP {}", pkt.target()); |
| 348 | } |
| 349 | Host targetHost = hosts.stream().findFirst().orElse(null); |
| 350 | /* |
| 351 | * If we know the host forward to its attachment |
| 352 | * point. |
| 353 | */ |
| 354 | if (targetHost != null) { |
| 355 | log.debug("Forward NDP request to the target host"); |
| 356 | pkt.forward(targetHost.location()); |
| 357 | } else { |
| 358 | /* |
| 359 | * Flood otherwise. |
| 360 | */ |
| 361 | log.debug("Flood NDP request to the target subnet"); |
| 362 | flood(pkt); |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Helper method to handle the ndp replies. |
| 369 | * |
| 370 | * @param pkt the ndp packet reply and context information |
| 371 | * @param hostService the host service |
| 372 | */ |
| 373 | private void handleNdpReply(NeighbourMessageContext pkt, HostService hostService) { |
| 374 | if (isNdpForGateway(pkt)) { |
| 375 | log.debug("Forwarding all the ip packets we stored"); |
| 376 | Ip6Address hostIpAddress = pkt.sender().getIp6Address(); |
| 377 | srManager.ipHandler.forwardPackets(pkt.inPort().deviceId(), hostIpAddress); |
| 378 | } else { |
| 379 | HostId hostId = HostId.hostId(pkt.dstMac(), pkt.vlan()); |
| 380 | Host targetHost = hostService.getHost(hostId); |
| 381 | if (targetHost != null) { |
| 382 | log.debug("Forwarding the reply to the host"); |
| 383 | pkt.forward(targetHost.location()); |
| 384 | } else { |
| 385 | /* |
| 386 | * We don't have to flood towards spine facing ports. |
| 387 | */ |
| 388 | if (pkt.vlan().equals(VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET))) { |
| 389 | return; |
| 390 | } |
| 391 | log.debug("Flooding the reply to the subnet"); |
| 392 | flood(pkt); |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Utility to verify if the ND are for the gateway. |
| 399 | * |
| 400 | * @param pkt the ndp packet |
| 401 | * @return true if the ndp is for the gateway. False otherwise |
| 402 | */ |
| 403 | private boolean isNdpForGateway(NeighbourMessageContext pkt) { |
| 404 | DeviceId deviceId = pkt.inPort().deviceId(); |
| 405 | Set<IpAddress> gatewayIpAddresses = null; |
| 406 | try { |
| 407 | if (pkt.target().equals(config.getRouterIpv6(deviceId))) { |
| 408 | return true; |
| 409 | } |
| 410 | gatewayIpAddresses = config.getPortIPs(deviceId); |
| 411 | } catch (DeviceConfigNotFoundException e) { |
| 412 | log.warn(e.getMessage() + " Aborting check for router IP in processing ndp"); |
| 413 | } |
| 414 | if (gatewayIpAddresses != null && |
| 415 | gatewayIpAddresses.contains(pkt.target())) { |
| 416 | return true; |
| 417 | } |
| 418 | return false; |
| 419 | } |
| 420 | |
| 421 | /** |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 422 | * 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] | 423 | * |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 424 | * @param deviceId Switch device ID |
| 425 | * @param targetAddress target IP address for ARP |
| 426 | * @param inPort in-port |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 427 | */ |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 428 | public void sendNdpRequest(DeviceId deviceId, IpAddress targetAddress, ConnectPoint inPort) { |
| 429 | byte[] senderMacAddress = new byte[MacAddress.MAC_ADDRESS_LENGTH]; |
| 430 | byte[] senderIpAddress = new byte[Ip6Address.BYTE_LENGTH]; |
| 431 | /* |
| 432 | * Retrieves device info. |
| 433 | */ |
| 434 | getSenderInfo(senderMacAddress, senderIpAddress, deviceId, targetAddress); |
| 435 | /* |
| 436 | * We have to compute the dst mac address and dst |
| 437 | * ip address. |
| 438 | */ |
| 439 | byte[] dstIp = IPv6.getSolicitNodeAddress(targetAddress.toOctets()); |
| 440 | byte[] dstMac = IPv6.getMCastMacAddress(dstIp); |
| 441 | /* |
| 442 | * Creates the request. |
| 443 | */ |
| 444 | Ethernet ndpRequest = NeighborSolicitation.buildNdpSolicit( |
| 445 | targetAddress.toOctets(), |
| 446 | senderIpAddress, |
| 447 | dstIp, |
| 448 | senderMacAddress, |
| 449 | dstMac, |
| 450 | VlanId.NONE |
| 451 | ); |
| 452 | flood(ndpRequest, inPort, targetAddress); |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 453 | } |
| 454 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 455 | } |