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; |
Pier Luigi | 37a3543 | 2017-02-01 13:50:04 -0800 | [diff] [blame] | 41 | import org.onosproject.net.packet.InboundPacket; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 42 | import org.onosproject.net.packet.OutboundPacket; |
Pier Luigi | 37a3543 | 2017-02-01 13:50:04 -0800 | [diff] [blame] | 43 | import org.onosproject.net.packet.PacketContext; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 44 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 45 | import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 46 | import org.slf4j.Logger; |
| 47 | import org.slf4j.LoggerFactory; |
| 48 | |
Jonathan Hart | d53ebc4 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 49 | import java.nio.ByteBuffer; |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 50 | import java.util.Set; |
Jonathan Hart | d53ebc4 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 51 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 52 | /** |
| 53 | * Handler of ICMP packets that responses or forwards ICMP packets that |
| 54 | * are sent to the controller. |
| 55 | */ |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 56 | public class IcmpHandler extends SegmentRoutingNeighbourHandler { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 57 | |
| 58 | private static Logger log = LoggerFactory.getLogger(IcmpHandler.class); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Creates an IcmpHandler object. |
| 62 | * |
| 63 | * @param srManager SegmentRoutingManager object |
| 64 | */ |
| 65 | public IcmpHandler(SegmentRoutingManager srManager) { |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 66 | super(srManager); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Utility function to send packet out. |
| 71 | * |
| 72 | * @param outport the output port |
| 73 | * @param payload the packet to send |
| 74 | * @param sid the segment id |
| 75 | * @param destIpAddress the destination ip address |
| 76 | * @param allowedHops the hop limit/ttl |
| 77 | */ |
| 78 | private void sendPacketOut(ConnectPoint outport, |
| 79 | Ethernet payload, |
| 80 | int sid, |
| 81 | IpAddress destIpAddress, |
| 82 | byte allowedHops) { |
| 83 | int destSid; |
| 84 | if (destIpAddress.isIp4()) { |
| 85 | destSid = config.getIPv4SegmentId(payload.getDestinationMAC()); |
| 86 | } else { |
| 87 | destSid = config.getIPv6SegmentId(payload.getDestinationMAC()); |
| 88 | } |
| 89 | |
| 90 | if (sid == -1 || destSid == sid || |
| 91 | config.inSameSubnet(outport.deviceId(), destIpAddress)) { |
| 92 | TrafficTreatment treatment = DefaultTrafficTreatment.builder(). |
| 93 | setOutput(outport.port()).build(); |
| 94 | OutboundPacket packet = new DefaultOutboundPacket(outport.deviceId(), |
| 95 | treatment, ByteBuffer.wrap(payload.serialize())); |
| 96 | srManager.packetService.emit(packet); |
| 97 | } else { |
| 98 | log.debug("Send a MPLS packet as a ICMP response"); |
| 99 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 100 | .setOutput(outport.port()) |
| 101 | .build(); |
| 102 | |
| 103 | payload.setEtherType(Ethernet.MPLS_UNICAST); |
| 104 | MPLS mplsPkt = new MPLS(); |
| 105 | mplsPkt.setLabel(sid); |
| 106 | mplsPkt.setTtl(allowedHops); |
| 107 | mplsPkt.setPayload(payload.getPayload()); |
| 108 | payload.setPayload(mplsPkt); |
| 109 | |
| 110 | OutboundPacket packet = new DefaultOutboundPacket(outport.deviceId(), |
| 111 | treatment, ByteBuffer.wrap(payload.serialize())); |
| 112 | |
| 113 | srManager.packetService.emit(packet); |
| 114 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 117 | ////////////////////////////////////// |
| 118 | // ICMP Echo/Reply Protocol // |
| 119 | ////////////////////////////////////// |
| 120 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 121 | /** |
| 122 | * Process incoming ICMP packet. |
| 123 | * If it is an ICMP request to router or known host, then sends an ICMP response. |
| 124 | * If it is an ICMP packet to known host and forward the packet to the host. |
| 125 | * If it is an ICMP packet to unknown host in a subnet, then sends an ARP request |
| 126 | * to the subnet. |
| 127 | * |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 128 | * @param eth inbound ICMP packet |
| 129 | * @param inPort the input port |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 130 | */ |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 131 | public void processIcmp(Ethernet eth, ConnectPoint inPort) { |
| 132 | DeviceId deviceId = inPort.deviceId(); |
| 133 | IPv4 ipv4Packet = (IPv4) eth.getPayload(); |
| 134 | Ip4Address destinationAddress = Ip4Address.valueOf(ipv4Packet.getDestinationAddress()); |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 135 | Set<IpAddress> gatewayIpAddresses = config.getPortIPs(deviceId); |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 136 | IpAddress routerIp; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 137 | try { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 138 | routerIp = config.getRouterIpv4(deviceId); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 139 | } catch (DeviceConfigNotFoundException e) { |
| 140 | log.warn(e.getMessage() + " Aborting processPacketIn."); |
| 141 | return; |
| 142 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 143 | // ICMP to the router IP or gateway IP |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 144 | if (((ICMP) ipv4Packet.getPayload()).getIcmpType() == ICMP.TYPE_ECHO_REQUEST && |
| 145 | (destinationAddress.equals(routerIp.getIp4Address()) || |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 146 | gatewayIpAddresses.contains(destinationAddress))) { |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 147 | sendIcmpResponse(eth, inPort); |
| 148 | // We remove the packet from the queue |
| 149 | srManager.ipHandler.dequeuePacket(ipv4Packet, destinationAddress); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 150 | |
| 151 | // ICMP for any known host |
| 152 | } else if (!srManager.hostService.getHostsByIp(destinationAddress).isEmpty()) { |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 153 | // TODO: known host packet should not be coming to controller - resend flows? |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 154 | srManager.ipHandler.forwardPackets(deviceId, destinationAddress); |
| 155 | |
| 156 | // ICMP for an unknown host in the subnet of the router |
| 157 | } else if (config.inSameSubnet(deviceId, destinationAddress)) { |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 158 | srManager.arpHandler.sendArpRequest(deviceId, destinationAddress, inPort); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 159 | |
| 160 | // ICMP for an unknown host |
| 161 | } else { |
| 162 | log.debug("ICMP request for unknown host {} ", destinationAddress); |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 163 | // We remove the packet from the queue |
| 164 | srManager.ipHandler.dequeuePacket(ipv4Packet, destinationAddress); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 168 | /** |
| 169 | * Sends an ICMP reply message. |
| 170 | * |
| 171 | * Note: we assume that packets sending from the edge switches to the hosts |
| 172 | * have untagged VLAN. |
| 173 | * @param icmpRequest the original ICMP request |
| 174 | * @param outport the output port where the ICMP reply should be sent to |
| 175 | */ |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 176 | private void sendIcmpResponse(Ethernet icmpRequest, ConnectPoint outport) { |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 177 | // Note: We assume that packets arrive at the edge switches have |
| 178 | // untagged VLAN. |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 179 | Ethernet icmpReplyEth = ICMP.buildIcmpReply(icmpRequest); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 180 | IPv4 icmpRequestIpv4 = (IPv4) icmpRequest.getPayload(); |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 181 | IPv4 icmpReplyIpv4 = (IPv4) icmpReplyEth.getPayload(); |
| 182 | Ip4Address destIpAddress = Ip4Address.valueOf(icmpRequestIpv4.getSourceAddress()); |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 183 | Ip4Address destRouterAddress = config.getRouterIpAddressForASubnetHost(destIpAddress); |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 184 | int destSid = config.getIPv4SegmentId(destRouterAddress); |
Charles Chan | 7066136 | 2016-12-09 12:54:49 -0800 | [diff] [blame] | 185 | if (destSid < 0) { |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 186 | log.warn("Cannot find the Segment ID for {}", destIpAddress); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 187 | return; |
| 188 | } |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 189 | sendPacketOut(outport, icmpReplyEth, destSid, destIpAddress, icmpReplyIpv4.getTtl()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 192 | /////////////////////////////////////////// |
| 193 | // ICMPv6 Echo/Reply Protocol // |
| 194 | /////////////////////////////////////////// |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 195 | |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 196 | /** |
| 197 | * Process incoming ICMPv6 packet. |
| 198 | * If it is an ICMP request to router or known host, then sends an ICMP response. |
| 199 | * If it is an ICMP packet to known host and forward the packet to the host. |
| 200 | * If it is an ICMP packet to unknown host in a subnet, then sends an ARP request |
| 201 | * to the subnet. |
| 202 | * |
| 203 | * @param eth the incoming ICMPv6 packet |
| 204 | * @param inPort the input port |
| 205 | */ |
| 206 | public void processIcmpv6(Ethernet eth, ConnectPoint inPort) { |
| 207 | DeviceId deviceId = inPort.deviceId(); |
| 208 | IPv6 ipv6Packet = (IPv6) eth.getPayload(); |
| 209 | Ip6Address destinationAddress = Ip6Address.valueOf(ipv6Packet.getDestinationAddress()); |
| 210 | Set<IpAddress> gatewayIpAddresses = config.getPortIPs(deviceId); |
| 211 | IpAddress routerIp; |
| 212 | try { |
| 213 | routerIp = config.getRouterIpv6(deviceId); |
| 214 | } catch (DeviceConfigNotFoundException e) { |
| 215 | log.warn(e.getMessage() + " Aborting processPacketIn."); |
| 216 | return; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 217 | } |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 218 | ICMP6 icmp6 = (ICMP6) ipv6Packet.getPayload(); |
| 219 | // ICMP to the router IP or gateway IP |
| 220 | if (icmp6.getIcmpType() == ICMP6.ECHO_REQUEST && |
| 221 | (destinationAddress.equals(routerIp.getIp6Address()) || |
| 222 | gatewayIpAddresses.contains(destinationAddress))) { |
| 223 | sendIcmpv6Response(eth, inPort); |
| 224 | // We remove the packet from the queue |
| 225 | srManager.ipHandler.dequeuePacket(ipv6Packet, destinationAddress); |
| 226 | // ICMP for any known host |
| 227 | } else if (!srManager.hostService.getHostsByIp(destinationAddress).isEmpty()) { |
| 228 | // TODO: known host packet should not be coming to controller - resend flows? |
| 229 | srManager.ipHandler.forwardPackets(deviceId, destinationAddress); |
| 230 | // ICMP for an unknown host in the subnet of the router |
| 231 | } else if (config.inSameSubnet(deviceId, destinationAddress)) { |
| 232 | sendNdpRequest(deviceId, destinationAddress, inPort); |
| 233 | // ICMP for an unknown host or not configured host |
| 234 | } else { |
| 235 | log.debug("ICMPv6 request for unknown host or not configured host {} ", destinationAddress); |
| 236 | // We remove the packet from the queue |
| 237 | srManager.ipHandler.dequeuePacket(ipv6Packet, destinationAddress); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Sends an ICMPv6 reply message. |
| 243 | * |
| 244 | * Note: we assume that packets sending from the edge switches to the hosts |
| 245 | * have untagged VLAN. |
| 246 | * @param ethRequest the original ICMP request |
| 247 | * @param outport the output port where the ICMP reply should be sent to |
| 248 | */ |
| 249 | private void sendIcmpv6Response(Ethernet ethRequest, ConnectPoint outport) { |
| 250 | // Note: We assume that packets arrive at the edge switches have |
| 251 | // untagged VLAN. |
| 252 | Ethernet ethReply = ICMP6.buildIcmp6Reply(ethRequest); |
| 253 | IPv6 icmpRequestIpv6 = (IPv6) ethRequest.getPayload(); |
| 254 | IPv6 icmpReplyIpv6 = (IPv6) ethRequest.getPayload(); |
| 255 | Ip6Address destIpAddress = Ip6Address.valueOf(icmpRequestIpv6.getSourceAddress()); |
| 256 | Ip6Address destRouterAddress = config.getRouterIpAddressForASubnetHost(destIpAddress); |
| 257 | int sid = config.getIPv6SegmentId(destRouterAddress); |
| 258 | if (sid < 0) { |
| 259 | log.warn("Cannot find the Segment ID for {}", destIpAddress); |
| 260 | return; |
| 261 | } |
| 262 | sendPacketOut(outport, ethReply, sid, destIpAddress, icmpReplyIpv6.getHopLimit()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 263 | } |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 264 | |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 265 | /////////////////////////////////////////// |
| 266 | // ICMPv6 Neighbour Discovery Protocol // |
| 267 | /////////////////////////////////////////// |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 268 | |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 269 | /** |
| 270 | * Process incoming NDP packet. |
| 271 | * |
| 272 | * If it is an NDP request for the router or for the gateway, then sends a NDP reply. |
| 273 | * If it is an NDP request to unknown host flood in the subnet. |
| 274 | * If it is an NDP packet to known host forward the packet to the host. |
| 275 | * |
| 276 | * FIXME If the NDP packets use link local addresses we fail. |
| 277 | * |
| 278 | * @param pkt inbound packet |
| 279 | * @param hostService the host service |
| 280 | */ |
| 281 | public void processPacketIn(NeighbourMessageContext pkt, HostService hostService) { |
| 282 | /* |
| 283 | * First we validate the ndp packet |
| 284 | */ |
| 285 | SegmentRoutingAppConfig appConfig = srManager.cfgService |
| 286 | .getConfig(srManager.appId, SegmentRoutingAppConfig.class); |
| 287 | if (appConfig != null && appConfig.suppressSubnet().contains(pkt.inPort())) { |
| 288 | // Ignore NDP packets come from suppressed ports |
| 289 | pkt.drop(); |
| 290 | return; |
| 291 | } |
| 292 | if (!validateSrcIp(pkt)) { |
| 293 | log.debug("Ignore NDP packet discovered on {} with unexpected src ip address {}.", |
| 294 | pkt.inPort(), pkt.sender()); |
| 295 | pkt.drop(); |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | if (pkt.type() == NeighbourMessageType.REQUEST) { |
| 300 | handleNdpRequest(pkt, hostService); |
| 301 | } else { |
| 302 | handleNdpReply(pkt, hostService); |
| 303 | } |
| 304 | |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Utility function to verify if the src ip belongs to the same |
| 309 | * subnet configured on the port it is seen. |
| 310 | * |
| 311 | * @param pkt the ndp packet and context information |
| 312 | * @return true if the src ip is a valid address for the subnet configured |
| 313 | * for the connect point |
| 314 | */ |
| 315 | private boolean validateSrcIp(NeighbourMessageContext pkt) { |
| 316 | ConnectPoint connectPoint = pkt.inPort(); |
| 317 | IpPrefix subnet = config.getPortIPv6Subnet( |
| 318 | connectPoint.deviceId(), |
| 319 | connectPoint.port() |
Pier Luigi | 6a2643a | 2017-01-31 09:35:05 -0800 | [diff] [blame] | 320 | ); |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 321 | return subnet != null && subnet.contains(pkt.sender()); |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Helper method to handle the ndp requests. |
| 326 | * |
| 327 | * @param pkt the ndp packet request and context information |
| 328 | * @param hostService the host service |
| 329 | */ |
| 330 | private void handleNdpRequest(NeighbourMessageContext pkt, HostService hostService) { |
| 331 | /* |
| 332 | * ND request for the gateway. We have to reply on behalf |
| 333 | * of the gateway. |
| 334 | */ |
| 335 | if (isNdpForGateway(pkt)) { |
| 336 | log.debug("Sending NDP reply on behalf of the router"); |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 337 | sendResponse(pkt, config.getRouterMacForAGatewayIp(pkt.target()), hostService); |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 338 | } else { |
| 339 | /* |
| 340 | * ND request for an host. We do a search by Ip. |
| 341 | */ |
| 342 | Set<Host> hosts = hostService.getHostsByIp(pkt.target()); |
| 343 | /* |
| 344 | * Possible misconfiguration ? In future this case |
| 345 | * should be handled we can have same hosts in different |
| 346 | * vlans. |
| 347 | */ |
| 348 | if (hosts.size() > 1) { |
| 349 | log.warn("More than one host with IP {}", pkt.target()); |
| 350 | } |
| 351 | Host targetHost = hosts.stream().findFirst().orElse(null); |
| 352 | /* |
| 353 | * If we know the host forward to its attachment |
| 354 | * point. |
| 355 | */ |
| 356 | if (targetHost != null) { |
| 357 | log.debug("Forward NDP request to the target host"); |
| 358 | pkt.forward(targetHost.location()); |
| 359 | } else { |
| 360 | /* |
| 361 | * Flood otherwise. |
| 362 | */ |
| 363 | log.debug("Flood NDP request to the target subnet"); |
| 364 | flood(pkt); |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Helper method to handle the ndp replies. |
| 371 | * |
| 372 | * @param pkt the ndp packet reply and context information |
| 373 | * @param hostService the host service |
| 374 | */ |
| 375 | private void handleNdpReply(NeighbourMessageContext pkt, HostService hostService) { |
| 376 | if (isNdpForGateway(pkt)) { |
| 377 | log.debug("Forwarding all the ip packets we stored"); |
| 378 | Ip6Address hostIpAddress = pkt.sender().getIp6Address(); |
| 379 | srManager.ipHandler.forwardPackets(pkt.inPort().deviceId(), hostIpAddress); |
| 380 | } else { |
| 381 | HostId hostId = HostId.hostId(pkt.dstMac(), pkt.vlan()); |
| 382 | Host targetHost = hostService.getHost(hostId); |
| 383 | if (targetHost != null) { |
| 384 | log.debug("Forwarding the reply to the host"); |
| 385 | pkt.forward(targetHost.location()); |
| 386 | } else { |
| 387 | /* |
| 388 | * We don't have to flood towards spine facing ports. |
| 389 | */ |
| 390 | if (pkt.vlan().equals(VlanId.vlanId(SegmentRoutingManager.ASSIGNED_VLAN_NO_SUBNET))) { |
| 391 | return; |
| 392 | } |
| 393 | log.debug("Flooding the reply to the subnet"); |
| 394 | flood(pkt); |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Utility to verify if the ND are for the gateway. |
| 401 | * |
| 402 | * @param pkt the ndp packet |
| 403 | * @return true if the ndp is for the gateway. False otherwise |
| 404 | */ |
| 405 | private boolean isNdpForGateway(NeighbourMessageContext pkt) { |
| 406 | DeviceId deviceId = pkt.inPort().deviceId(); |
| 407 | Set<IpAddress> gatewayIpAddresses = null; |
| 408 | try { |
| 409 | if (pkt.target().equals(config.getRouterIpv6(deviceId))) { |
| 410 | return true; |
| 411 | } |
| 412 | gatewayIpAddresses = config.getPortIPs(deviceId); |
| 413 | } catch (DeviceConfigNotFoundException e) { |
| 414 | log.warn(e.getMessage() + " Aborting check for router IP in processing ndp"); |
| 415 | } |
| 416 | if (gatewayIpAddresses != null && |
| 417 | gatewayIpAddresses.contains(pkt.target())) { |
| 418 | return true; |
| 419 | } |
| 420 | return false; |
| 421 | } |
| 422 | |
| 423 | /** |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 424 | * 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] | 425 | * |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 426 | * @param deviceId Switch device ID |
| 427 | * @param targetAddress target IP address for ARP |
| 428 | * @param inPort in-port |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 429 | */ |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 430 | public void sendNdpRequest(DeviceId deviceId, IpAddress targetAddress, ConnectPoint inPort) { |
| 431 | byte[] senderMacAddress = new byte[MacAddress.MAC_ADDRESS_LENGTH]; |
| 432 | byte[] senderIpAddress = new byte[Ip6Address.BYTE_LENGTH]; |
| 433 | /* |
| 434 | * Retrieves device info. |
| 435 | */ |
Pier Luigi | 6a83c4a | 2017-01-29 12:38:48 -0800 | [diff] [blame] | 436 | if (!getSenderInfo(senderMacAddress, senderIpAddress, deviceId, targetAddress)) { |
| 437 | log.warn("Aborting sendNdpRequest, we cannot get all the information needed"); |
| 438 | return; |
| 439 | } |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 440 | /* |
| 441 | * We have to compute the dst mac address and dst |
| 442 | * ip address. |
| 443 | */ |
| 444 | byte[] dstIp = IPv6.getSolicitNodeAddress(targetAddress.toOctets()); |
| 445 | byte[] dstMac = IPv6.getMCastMacAddress(dstIp); |
| 446 | /* |
| 447 | * Creates the request. |
| 448 | */ |
| 449 | Ethernet ndpRequest = NeighborSolicitation.buildNdpSolicit( |
| 450 | targetAddress.toOctets(), |
| 451 | senderIpAddress, |
| 452 | dstIp, |
| 453 | senderMacAddress, |
| 454 | dstMac, |
| 455 | VlanId.NONE |
| 456 | ); |
| 457 | flood(ndpRequest, inPort, targetAddress); |
Pier Ventre | 1a65596 | 2016-11-28 16:48:06 -0800 | [diff] [blame] | 458 | } |
| 459 | |
Pier Luigi | 37a3543 | 2017-02-01 13:50:04 -0800 | [diff] [blame] | 460 | ///////////////////////////////////////////////////////////////// |
| 461 | // XXX Neighbour hacking, temporary workaround will be // |
| 462 | // removed as soon as possible, when the bridging will // |
| 463 | // be implemented. For now, it's fine to leave this // |
| 464 | ///////////////////////////////////////////////////////////////// |
| 465 | |
Pier Luigi | 37a3543 | 2017-02-01 13:50:04 -0800 | [diff] [blame] | 466 | // XXX Neighbour hacking, this method is used to handle |
| 467 | // the ICMPv6 protocols for the upstream port |
| 468 | public boolean handleUPstreamPackets(PacketContext packetContext) { |
| 469 | InboundPacket pkt = packetContext.inPacket(); |
| 470 | Ethernet ethernet = pkt.parsed(); |
Pier Luigi | ad0a67f | 2017-02-03 13:34:21 -0800 | [diff] [blame] | 471 | if (srManager.vRouterCP == null || srManager.upstreamCP == null) { |
Pier Luigi | 37a3543 | 2017-02-01 13:50:04 -0800 | [diff] [blame] | 472 | return false; |
| 473 | } |
Pier Luigi | ad0a67f | 2017-02-03 13:34:21 -0800 | [diff] [blame] | 474 | if (pkt.receivedFrom().equals(srManager.upstreamCP)) { |
| 475 | sendTo(ByteBuffer.wrap(ethernet.serialize()), srManager.vRouterCP); |
Pier Luigi | 37a3543 | 2017-02-01 13:50:04 -0800 | [diff] [blame] | 476 | return true; |
| 477 | } |
| 478 | return false; |
| 479 | } |
| 480 | |
Pier Luigi | 37a3543 | 2017-02-01 13:50:04 -0800 | [diff] [blame] | 481 | // XXX Neigbour hack. To send out a packet |
| 482 | private void sendTo(ByteBuffer packet, ConnectPoint outPort) { |
| 483 | TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder(); |
| 484 | builder.setOutput(outPort.port()); |
| 485 | srManager.packetService.emit(new DefaultOutboundPacket(outPort.deviceId(), |
| 486 | builder.build(), packet)); |
| 487 | } |
| 488 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 489 | } |