sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
| 18 | import java.nio.ByteBuffer; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 19 | import java.util.List; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 20 | import org.onlab.packet.Ethernet; |
| 21 | import org.onlab.packet.ICMP; |
| 22 | import org.onlab.packet.IPv4; |
| 23 | import org.onlab.packet.Ip4Address; |
| 24 | import org.onlab.packet.IpPrefix; |
| 25 | import org.onlab.packet.MPLS; |
| 26 | import org.onosproject.net.ConnectPoint; |
| 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 29 | import org.onosproject.net.flow.TrafficTreatment; |
| 30 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 31 | import org.onosproject.net.packet.InboundPacket; |
| 32 | import org.onosproject.net.packet.OutboundPacket; |
| 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
| 36 | import static com.google.common.base.Preconditions.checkNotNull; |
| 37 | |
| 38 | public class IcmpHandler { |
| 39 | |
| 40 | private static Logger log = LoggerFactory.getLogger(IcmpHandler.class); |
| 41 | private SegmentRoutingManager srManager; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 42 | private DeviceConfiguration config; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Creates an IcmpHandler object. |
| 46 | * |
| 47 | * @param srManager SegmentRoutingManager object |
| 48 | */ |
| 49 | public IcmpHandler(SegmentRoutingManager srManager) { |
| 50 | this.srManager = srManager; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 51 | this.config = checkNotNull(srManager.deviceConfiguration); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Process incoming ICMP packet. |
| 56 | * If it is an ICMP request to router or known host, then sends an ICMP response. |
| 57 | * If it is an ICMP packet to known host and forward the packet to the host. |
| 58 | * If it is an ICMP packet to unknown host in a subnet, then sends an ARP request |
| 59 | * to the subnet. |
| 60 | * |
Thomas Vachuska | 8a07509 | 2015-04-15 18:20:08 -0700 | [diff] [blame] | 61 | * @param pkt inbound packet |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 62 | */ |
| 63 | public void processPacketIn(InboundPacket pkt) { |
| 64 | |
| 65 | Ethernet ethernet = pkt.parsed(); |
| 66 | IPv4 ipv4 = (IPv4) ethernet.getPayload(); |
| 67 | |
| 68 | ConnectPoint connectPoint = pkt.receivedFrom(); |
| 69 | DeviceId deviceId = connectPoint.deviceId(); |
| 70 | Ip4Address destinationAddress = |
| 71 | Ip4Address.valueOf(ipv4.getDestinationAddress()); |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 72 | List<Ip4Address> gatewayIpAddresses = config.getSubnetGatewayIps(deviceId); |
| 73 | Ip4Address routerIp = config.getRouterIp(deviceId); |
| 74 | IpPrefix routerIpPrefix = IpPrefix.valueOf(routerIp, IpPrefix.MAX_INET_MASK_LENGTH); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 75 | Ip4Address routerIpAddress = routerIpPrefix.getIp4Prefix().address(); |
| 76 | |
| 77 | // ICMP to the router IP or gateway IP |
| 78 | if (((ICMP) ipv4.getPayload()).getIcmpType() == ICMP.TYPE_ECHO_REQUEST && |
| 79 | (destinationAddress.equals(routerIpAddress) || |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 80 | gatewayIpAddresses.contains(destinationAddress))) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 81 | sendICMPResponse(ethernet, connectPoint); |
| 82 | // TODO: do we need to set the flow rule again ?? |
| 83 | |
| 84 | // ICMP for any known host |
| 85 | } else if (!srManager.hostService.getHostsByIp(destinationAddress).isEmpty()) { |
| 86 | srManager.ipHandler.forwardPackets(deviceId, destinationAddress); |
| 87 | |
| 88 | // ICMP for an unknown host in the subnet of the router |
| 89 | } else if (config.inSameSubnet(deviceId, destinationAddress)) { |
| 90 | srManager.arpHandler.sendArpRequest(deviceId, destinationAddress, connectPoint); |
| 91 | |
| 92 | // ICMP for an unknown host |
| 93 | } else { |
| 94 | log.debug("ICMP request for unknown host {} ", destinationAddress); |
| 95 | // Do nothing |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | private void sendICMPResponse(Ethernet icmpRequest, ConnectPoint outport) { |
| 100 | |
| 101 | Ethernet icmpReplyEth = new Ethernet(); |
| 102 | |
| 103 | IPv4 icmpRequestIpv4 = (IPv4) icmpRequest.getPayload(); |
| 104 | IPv4 icmpReplyIpv4 = new IPv4(); |
| 105 | |
| 106 | int destAddress = icmpRequestIpv4.getDestinationAddress(); |
| 107 | icmpReplyIpv4.setDestinationAddress(icmpRequestIpv4.getSourceAddress()); |
| 108 | icmpReplyIpv4.setSourceAddress(destAddress); |
| 109 | icmpReplyIpv4.setTtl((byte) 64); |
| 110 | icmpReplyIpv4.setChecksum((short) 0); |
| 111 | |
| 112 | ICMP icmpReply = (ICMP) icmpRequestIpv4.getPayload().clone(); |
| 113 | icmpReply.setIcmpType(ICMP.TYPE_ECHO_REPLY); |
| 114 | icmpReply.setIcmpCode(ICMP.SUBTYPE_ECHO_REPLY); |
| 115 | icmpReply.setChecksum((short) 0); |
| 116 | |
| 117 | icmpReplyIpv4.setPayload(icmpReply); |
| 118 | |
| 119 | icmpReplyEth.setPayload(icmpReplyIpv4); |
| 120 | icmpReplyEth.setEtherType(Ethernet.TYPE_IPV4); |
| 121 | icmpReplyEth.setDestinationMACAddress(icmpRequest.getSourceMACAddress()); |
| 122 | icmpReplyEth.setSourceMACAddress(icmpRequest.getDestinationMACAddress()); |
| 123 | icmpReplyEth.setVlanID(icmpRequest.getVlanID()); |
| 124 | |
| 125 | Ip4Address destIpAddress = Ip4Address.valueOf(icmpReplyIpv4.getDestinationAddress()); |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 126 | Ip4Address destRouterAddress = config.getRouterIpAddressForASubnetHost(destIpAddress); |
| 127 | int sid = config.getSegmentId(destRouterAddress); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 128 | if (sid < 0) { |
| 129 | log.warn("Cannot find the Segment ID for {}", destAddress); |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | sendPacketOut(outport, icmpReplyEth, sid); |
| 134 | |
| 135 | } |
| 136 | |
| 137 | private void sendPacketOut(ConnectPoint outport, Ethernet payload, int sid) { |
| 138 | |
| 139 | IPv4 ipPacket = (IPv4) payload.getPayload(); |
| 140 | Ip4Address destIpAddress = Ip4Address.valueOf(ipPacket.getDestinationAddress()); |
| 141 | |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 142 | if (sid == -1 || config.getSegmentId(payload.getDestinationMAC()) == sid || |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 143 | config.inSameSubnet(outport.deviceId(), destIpAddress)) { |
| 144 | TrafficTreatment treatment = DefaultTrafficTreatment.builder(). |
| 145 | setOutput(outport.port()).build(); |
| 146 | OutboundPacket packet = new DefaultOutboundPacket(outport.deviceId(), |
| 147 | treatment, ByteBuffer.wrap(payload.serialize())); |
| 148 | srManager.packetService.emit(packet); |
| 149 | } else { |
| 150 | log.warn("Send a MPLS packet as a ICMP response"); |
| 151 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 152 | .setOutput(outport.port()) |
| 153 | .build(); |
| 154 | |
| 155 | payload.setEtherType(Ethernet.MPLS_UNICAST); |
| 156 | MPLS mplsPkt = new MPLS(); |
| 157 | mplsPkt.setLabel(sid); |
| 158 | mplsPkt.setTtl(((IPv4) payload.getPayload()).getTtl()); |
| 159 | mplsPkt.setPayload(payload.getPayload()); |
| 160 | payload.setPayload(mplsPkt); |
| 161 | |
| 162 | OutboundPacket packet = new DefaultOutboundPacket(outport.deviceId(), |
| 163 | treatment, ByteBuffer.wrap(payload.serialize())); |
| 164 | |
| 165 | srManager.packetService.emit(packet); |
| 166 | } |
| 167 | } |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 168 | |
| 169 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 170 | } |