blob: b3da8dcb23eb3a017f03ef57502b37fb293b5cc1 [file] [log] [blame]
sangho80f11cb2015-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
18import java.nio.ByteBuffer;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070019import java.util.List;
sangho80f11cb2015-04-01 13:05:26 -070020import org.onlab.packet.Ethernet;
21import org.onlab.packet.ICMP;
22import org.onlab.packet.IPv4;
23import org.onlab.packet.Ip4Address;
24import org.onlab.packet.IpPrefix;
25import org.onlab.packet.MPLS;
26import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.flow.DefaultTrafficTreatment;
29import org.onosproject.net.flow.TrafficTreatment;
30import org.onosproject.net.packet.DefaultOutboundPacket;
31import org.onosproject.net.packet.InboundPacket;
32import org.onosproject.net.packet.OutboundPacket;
33import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
36import static com.google.common.base.Preconditions.checkNotNull;
37
38public class IcmpHandler {
39
40 private static Logger log = LoggerFactory.getLogger(IcmpHandler.class);
41 private SegmentRoutingManager srManager;
sangho9b169e32015-04-14 16:27:13 -070042 private DeviceConfiguration config;
sangho80f11cb2015-04-01 13:05:26 -070043
44 /**
45 * Creates an IcmpHandler object.
46 *
47 * @param srManager SegmentRoutingManager object
48 */
49 public IcmpHandler(SegmentRoutingManager srManager) {
50 this.srManager = srManager;
sangho9b169e32015-04-14 16:27:13 -070051 this.config = checkNotNull(srManager.deviceConfiguration);
sangho80f11cb2015-04-01 13:05:26 -070052 }
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 Vachuska8a075092015-04-15 18:20:08 -070061 * @param pkt inbound packet
sangho80f11cb2015-04-01 13:05:26 -070062 */
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());
sangho9b169e32015-04-14 16:27:13 -070072 List<Ip4Address> gatewayIpAddresses = config.getSubnetGatewayIps(deviceId);
73 Ip4Address routerIp = config.getRouterIp(deviceId);
74 IpPrefix routerIpPrefix = IpPrefix.valueOf(routerIp, IpPrefix.MAX_INET_MASK_LENGTH);
sangho80f11cb2015-04-01 13:05:26 -070075 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 Vavilapalli37a461b2015-04-07 15:12:32 -070080 gatewayIpAddresses.contains(destinationAddress))) {
sangho80f11cb2015-04-01 13:05:26 -070081 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());
sangho9b169e32015-04-14 16:27:13 -0700126 Ip4Address destRouterAddress = config.getRouterIpAddressForASubnetHost(destIpAddress);
127 int sid = config.getSegmentId(destRouterAddress);
sangho80f11cb2015-04-01 13:05:26 -0700128 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
sangho9b169e32015-04-14 16:27:13 -0700142 if (sid == -1 || config.getSegmentId(payload.getDestinationMAC()) == sid ||
sangho80f11cb2015-04-01 13:05:26 -0700143 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 }
sangho9b169e32015-04-14 16:27:13 -0700168
169
sangho80f11cb2015-04-01 13:05:26 -0700170}