blob: c4267ebbd25a4905ad97145bfff893288bbbaebb [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
sangho80f11cb2015-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;
Charles Chan319d1a22015-11-03 10:42:14 -080031import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
32import org.onosproject.segmentrouting.config.DeviceConfiguration;
sangho80f11cb2015-04-01 13:05:26 -070033import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
Jonathan Hartd53ebc42015-04-07 16:46:33 -070036import java.nio.ByteBuffer;
Saurav Dasc28b3432015-10-30 17:45:38 -070037import java.util.Set;
Jonathan Hartd53ebc42015-04-07 16:46:33 -070038
sangho80f11cb2015-04-01 13:05:26 -070039import static com.google.common.base.Preconditions.checkNotNull;
40
41public class IcmpHandler {
42
43 private static Logger log = LoggerFactory.getLogger(IcmpHandler.class);
44 private SegmentRoutingManager srManager;
sangho9b169e32015-04-14 16:27:13 -070045 private DeviceConfiguration config;
sangho80f11cb2015-04-01 13:05:26 -070046
47 /**
48 * Creates an IcmpHandler object.
49 *
50 * @param srManager SegmentRoutingManager object
51 */
52 public IcmpHandler(SegmentRoutingManager srManager) {
53 this.srManager = srManager;
sangho9b169e32015-04-14 16:27:13 -070054 this.config = checkNotNull(srManager.deviceConfiguration);
sangho80f11cb2015-04-01 13:05:26 -070055 }
56
57 /**
58 * Process incoming ICMP packet.
59 * If it is an ICMP request to router or known host, then sends an ICMP response.
60 * If it is an ICMP packet to known host and forward the packet to the host.
61 * If it is an ICMP packet to unknown host in a subnet, then sends an ARP request
62 * to the subnet.
63 *
Thomas Vachuska8a075092015-04-15 18:20:08 -070064 * @param pkt inbound packet
sangho80f11cb2015-04-01 13:05:26 -070065 */
66 public void processPacketIn(InboundPacket pkt) {
67
68 Ethernet ethernet = pkt.parsed();
69 IPv4 ipv4 = (IPv4) ethernet.getPayload();
70
71 ConnectPoint connectPoint = pkt.receivedFrom();
72 DeviceId deviceId = connectPoint.deviceId();
73 Ip4Address destinationAddress =
74 Ip4Address.valueOf(ipv4.getDestinationAddress());
Saurav Dasc28b3432015-10-30 17:45:38 -070075 Set<Ip4Address> gatewayIpAddresses = config.getPortIPs(deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -080076 Ip4Address routerIp;
77 try {
78 routerIp = config.getRouterIp(deviceId);
79 } catch (DeviceConfigNotFoundException e) {
80 log.warn(e.getMessage() + " Aborting processPacketIn.");
81 return;
82 }
sangho9b169e32015-04-14 16:27:13 -070083 IpPrefix routerIpPrefix = IpPrefix.valueOf(routerIp, IpPrefix.MAX_INET_MASK_LENGTH);
sangho80f11cb2015-04-01 13:05:26 -070084 Ip4Address routerIpAddress = routerIpPrefix.getIp4Prefix().address();
85
86 // ICMP to the router IP or gateway IP
87 if (((ICMP) ipv4.getPayload()).getIcmpType() == ICMP.TYPE_ECHO_REQUEST &&
88 (destinationAddress.equals(routerIpAddress) ||
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070089 gatewayIpAddresses.contains(destinationAddress))) {
sangho80f11cb2015-04-01 13:05:26 -070090 sendICMPResponse(ethernet, connectPoint);
91 // TODO: do we need to set the flow rule again ??
92
93 // ICMP for any known host
94 } else if (!srManager.hostService.getHostsByIp(destinationAddress).isEmpty()) {
95 srManager.ipHandler.forwardPackets(deviceId, destinationAddress);
96
97 // ICMP for an unknown host in the subnet of the router
98 } else if (config.inSameSubnet(deviceId, destinationAddress)) {
99 srManager.arpHandler.sendArpRequest(deviceId, destinationAddress, connectPoint);
100
101 // ICMP for an unknown host
102 } else {
103 log.debug("ICMP request for unknown host {} ", destinationAddress);
104 // Do nothing
105 }
106 }
107
108 private void sendICMPResponse(Ethernet icmpRequest, ConnectPoint outport) {
109
110 Ethernet icmpReplyEth = new Ethernet();
111
112 IPv4 icmpRequestIpv4 = (IPv4) icmpRequest.getPayload();
113 IPv4 icmpReplyIpv4 = new IPv4();
114
115 int destAddress = icmpRequestIpv4.getDestinationAddress();
116 icmpReplyIpv4.setDestinationAddress(icmpRequestIpv4.getSourceAddress());
117 icmpReplyIpv4.setSourceAddress(destAddress);
118 icmpReplyIpv4.setTtl((byte) 64);
119 icmpReplyIpv4.setChecksum((short) 0);
120
Jonathan Hartd53ebc42015-04-07 16:46:33 -0700121 ICMP icmpReply = new ICMP();
Saurav Dasa5bb7cb2015-09-30 10:00:49 -0700122 icmpReply.setPayload(((ICMP) icmpRequestIpv4.getPayload()).getPayload());
sangho80f11cb2015-04-01 13:05:26 -0700123 icmpReply.setIcmpType(ICMP.TYPE_ECHO_REPLY);
124 icmpReply.setIcmpCode(ICMP.SUBTYPE_ECHO_REPLY);
125 icmpReply.setChecksum((short) 0);
sangho80f11cb2015-04-01 13:05:26 -0700126 icmpReplyIpv4.setPayload(icmpReply);
127
128 icmpReplyEth.setPayload(icmpReplyIpv4);
129 icmpReplyEth.setEtherType(Ethernet.TYPE_IPV4);
130 icmpReplyEth.setDestinationMACAddress(icmpRequest.getSourceMACAddress());
131 icmpReplyEth.setSourceMACAddress(icmpRequest.getDestinationMACAddress());
132 icmpReplyEth.setVlanID(icmpRequest.getVlanID());
133
134 Ip4Address destIpAddress = Ip4Address.valueOf(icmpReplyIpv4.getDestinationAddress());
sangho9b169e32015-04-14 16:27:13 -0700135 Ip4Address destRouterAddress = config.getRouterIpAddressForASubnetHost(destIpAddress);
136 int sid = config.getSegmentId(destRouterAddress);
sangho80f11cb2015-04-01 13:05:26 -0700137 if (sid < 0) {
138 log.warn("Cannot find the Segment ID for {}", destAddress);
139 return;
140 }
141
142 sendPacketOut(outport, icmpReplyEth, sid);
143
144 }
145
146 private void sendPacketOut(ConnectPoint outport, Ethernet payload, int sid) {
147
148 IPv4 ipPacket = (IPv4) payload.getPayload();
149 Ip4Address destIpAddress = Ip4Address.valueOf(ipPacket.getDestinationAddress());
150
sangho9b169e32015-04-14 16:27:13 -0700151 if (sid == -1 || config.getSegmentId(payload.getDestinationMAC()) == sid ||
sangho80f11cb2015-04-01 13:05:26 -0700152 config.inSameSubnet(outport.deviceId(), destIpAddress)) {
153 TrafficTreatment treatment = DefaultTrafficTreatment.builder().
154 setOutput(outport.port()).build();
155 OutboundPacket packet = new DefaultOutboundPacket(outport.deviceId(),
156 treatment, ByteBuffer.wrap(payload.serialize()));
157 srManager.packetService.emit(packet);
158 } else {
159 log.warn("Send a MPLS packet as a ICMP response");
160 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
161 .setOutput(outport.port())
162 .build();
163
164 payload.setEtherType(Ethernet.MPLS_UNICAST);
165 MPLS mplsPkt = new MPLS();
166 mplsPkt.setLabel(sid);
167 mplsPkt.setTtl(((IPv4) payload.getPayload()).getTtl());
168 mplsPkt.setPayload(payload.getPayload());
169 payload.setPayload(mplsPkt);
170
171 OutboundPacket packet = new DefaultOutboundPacket(outport.deviceId(),
172 treatment, ByteBuffer.wrap(payload.serialize()));
173
174 srManager.packetService.emit(packet);
175 }
176 }
sangho9b169e32015-04-14 16:27:13 -0700177
178
sangho80f11cb2015-04-01 13:05:26 -0700179}