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