blob: 45fa794563f030c6ae7fcf38451979f73f10f755 [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
18import java.nio.ByteBuffer;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070019import java.util.List;
sanghob35a6192015-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;
42 private NetworkConfigHandler config;
43
44 /**
45 * Creates an IcmpHandler object.
46 *
47 * @param srManager SegmentRoutingManager object
48 */
49 public IcmpHandler(SegmentRoutingManager srManager) {
50 this.srManager = srManager;
51 this.config = checkNotNull(srManager.networkConfigHandler);
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 *
61 * @param pkt
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());
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070072 List<Ip4Address> gatewayIpAddresses = config.getGatewayIpAddress(deviceId);
sanghob35a6192015-04-01 13:05:26 -070073 IpPrefix routerIpPrefix = config.getRouterIpAddress(deviceId);
74 Ip4Address routerIpAddress = routerIpPrefix.getIp4Prefix().address();
75
76 // ICMP to the router IP or gateway IP
77 if (((ICMP) ipv4.getPayload()).getIcmpType() == ICMP.TYPE_ECHO_REQUEST &&
78 (destinationAddress.equals(routerIpAddress) ||
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070079 gatewayIpAddresses.contains(destinationAddress))) {
sanghob35a6192015-04-01 13:05:26 -070080 sendICMPResponse(ethernet, connectPoint);
81 // TODO: do we need to set the flow rule again ??
82
83 // ICMP for any known host
84 } else if (!srManager.hostService.getHostsByIp(destinationAddress).isEmpty()) {
85 srManager.ipHandler.forwardPackets(deviceId, destinationAddress);
86
87 // ICMP for an unknown host in the subnet of the router
88 } else if (config.inSameSubnet(deviceId, destinationAddress)) {
89 srManager.arpHandler.sendArpRequest(deviceId, destinationAddress, connectPoint);
90
91 // ICMP for an unknown host
92 } else {
93 log.debug("ICMP request for unknown host {} ", destinationAddress);
94 // Do nothing
95 }
96 }
97
98 private void sendICMPResponse(Ethernet icmpRequest, ConnectPoint outport) {
99
100 Ethernet icmpReplyEth = new Ethernet();
101
102 IPv4 icmpRequestIpv4 = (IPv4) icmpRequest.getPayload();
103 IPv4 icmpReplyIpv4 = new IPv4();
104
105 int destAddress = icmpRequestIpv4.getDestinationAddress();
106 icmpReplyIpv4.setDestinationAddress(icmpRequestIpv4.getSourceAddress());
107 icmpReplyIpv4.setSourceAddress(destAddress);
108 icmpReplyIpv4.setTtl((byte) 64);
109 icmpReplyIpv4.setChecksum((short) 0);
110
111 ICMP icmpReply = (ICMP) icmpRequestIpv4.getPayload().clone();
112 icmpReply.setIcmpType(ICMP.TYPE_ECHO_REPLY);
113 icmpReply.setIcmpCode(ICMP.SUBTYPE_ECHO_REPLY);
114 icmpReply.setChecksum((short) 0);
115
116 icmpReplyIpv4.setPayload(icmpReply);
117
118 icmpReplyEth.setPayload(icmpReplyIpv4);
119 icmpReplyEth.setEtherType(Ethernet.TYPE_IPV4);
120 icmpReplyEth.setDestinationMACAddress(icmpRequest.getSourceMACAddress());
121 icmpReplyEth.setSourceMACAddress(icmpRequest.getDestinationMACAddress());
122 icmpReplyEth.setVlanID(icmpRequest.getVlanID());
123
124 Ip4Address destIpAddress = Ip4Address.valueOf(icmpReplyIpv4.getDestinationAddress());
125 Ip4Address destRouterAddress = config.getDestinationRouterAddress(destIpAddress);
126 int sid = config.getMplsId(destRouterAddress);
127 if (sid < 0) {
128 log.warn("Cannot find the Segment ID for {}", destAddress);
129 return;
130 }
131
132 sendPacketOut(outport, icmpReplyEth, sid);
133
134 }
135
136 private void sendPacketOut(ConnectPoint outport, Ethernet payload, int sid) {
137
138 IPv4 ipPacket = (IPv4) payload.getPayload();
139 Ip4Address destIpAddress = Ip4Address.valueOf(ipPacket.getDestinationAddress());
140
141 if (sid == -1 || config.getMplsId(payload.getDestinationMAC()) == sid ||
142 config.inSameSubnet(outport.deviceId(), destIpAddress)) {
143 TrafficTreatment treatment = DefaultTrafficTreatment.builder().
144 setOutput(outport.port()).build();
145 OutboundPacket packet = new DefaultOutboundPacket(outport.deviceId(),
146 treatment, ByteBuffer.wrap(payload.serialize()));
147 srManager.packetService.emit(packet);
148 } else {
149 log.warn("Send a MPLS packet as a ICMP response");
150 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
151 .setOutput(outport.port())
152 .build();
153
154 payload.setEtherType(Ethernet.MPLS_UNICAST);
155 MPLS mplsPkt = new MPLS();
156 mplsPkt.setLabel(sid);
157 mplsPkt.setTtl(((IPv4) payload.getPayload()).getTtl());
158 mplsPkt.setPayload(payload.getPayload());
159 payload.setPayload(mplsPkt);
160
161 OutboundPacket packet = new DefaultOutboundPacket(outport.deviceId(),
162 treatment, ByteBuffer.wrap(payload.serialize()));
163
164 srManager.packetService.emit(packet);
165 }
166 }
167}