eb3b3fd564e0d7d5abba9b15267564217fff0a13
[onosfw.git] /
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 org.onlab.packet.Ethernet;
19 import org.onlab.packet.ICMP;
20 import org.onlab.packet.IPv4;
21 import org.onlab.packet.Ip4Address;
22 import org.onlab.packet.IpPrefix;
23 import org.onlab.packet.MPLS;
24 import org.onosproject.net.ConnectPoint;
25 import org.onosproject.net.DeviceId;
26 import org.onosproject.net.flow.DefaultTrafficTreatment;
27 import org.onosproject.net.flow.TrafficTreatment;
28 import org.onosproject.net.packet.DefaultOutboundPacket;
29 import org.onosproject.net.packet.InboundPacket;
30 import org.onosproject.net.packet.OutboundPacket;
31 import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
32 import org.onosproject.segmentrouting.config.DeviceConfiguration;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import java.nio.ByteBuffer;
37 import java.util.Set;
38
39 import static com.google.common.base.Preconditions.checkNotNull;
40
41 public class IcmpHandler {
42
43     private static Logger log = LoggerFactory.getLogger(IcmpHandler.class);
44     private SegmentRoutingManager srManager;
45     private DeviceConfiguration config;
46
47     /**
48      * Creates an IcmpHandler object.
49      *
50      * @param srManager SegmentRoutingManager object
51      */
52     public IcmpHandler(SegmentRoutingManager srManager) {
53         this.srManager = srManager;
54         this.config = checkNotNull(srManager.deviceConfiguration);
55     }
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      *
64      * @param pkt inbound packet
65      */
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());
75         Set<Ip4Address> gatewayIpAddresses = config.getPortIPs(deviceId);
76         Ip4Address routerIp;
77         try {
78             routerIp = config.getRouterIp(deviceId);
79         } catch (DeviceConfigNotFoundException e) {
80             log.warn(e.getMessage() + " Aborting processPacketIn.");
81             return;
82         }
83         IpPrefix routerIpPrefix = IpPrefix.valueOf(routerIp, IpPrefix.MAX_INET_MASK_LENGTH);
84         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) ||
89                         gatewayIpAddresses.contains(destinationAddress))) {
90             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     /**
109      * Sends an ICMP reply message.
110      *
111      * Note: we assume that packets sending from the edge switches to the hosts
112      * have untagged VLAN.
113      * @param icmpRequest the original ICMP request
114      * @param outport the output port where the ICMP reply should be sent to
115      */
116     private void sendICMPResponse(Ethernet icmpRequest, ConnectPoint outport) {
117         // Note: We assume that packets arrive at the edge switches have
118         // untagged VLAN.
119         Ethernet icmpReplyEth = new Ethernet();
120
121         IPv4 icmpRequestIpv4 = (IPv4) icmpRequest.getPayload();
122         IPv4 icmpReplyIpv4 = new IPv4();
123
124         int destAddress = icmpRequestIpv4.getDestinationAddress();
125         icmpReplyIpv4.setDestinationAddress(icmpRequestIpv4.getSourceAddress());
126         icmpReplyIpv4.setSourceAddress(destAddress);
127         icmpReplyIpv4.setTtl((byte) 64);
128         icmpReplyIpv4.setChecksum((short) 0);
129
130         ICMP icmpReply = new ICMP();
131         icmpReply.setPayload(((ICMP) icmpRequestIpv4.getPayload()).getPayload());
132         icmpReply.setIcmpType(ICMP.TYPE_ECHO_REPLY);
133         icmpReply.setIcmpCode(ICMP.SUBTYPE_ECHO_REPLY);
134         icmpReply.setChecksum((short) 0);
135         icmpReplyIpv4.setPayload(icmpReply);
136
137         icmpReplyEth.setPayload(icmpReplyIpv4);
138         icmpReplyEth.setEtherType(Ethernet.TYPE_IPV4);
139         icmpReplyEth.setDestinationMACAddress(icmpRequest.getSourceMACAddress());
140         icmpReplyEth.setSourceMACAddress(icmpRequest.getDestinationMACAddress());
141
142         Ip4Address destIpAddress = Ip4Address.valueOf(icmpReplyIpv4.getDestinationAddress());
143         Ip4Address destRouterAddress = config.getRouterIpAddressForASubnetHost(destIpAddress);
144         int sid = config.getSegmentId(destRouterAddress);
145         if (sid < 0) {
146             log.warn("Cannot find the Segment ID for {}", destAddress);
147             return;
148         }
149
150         sendPacketOut(outport, icmpReplyEth, sid);
151
152     }
153
154     private void sendPacketOut(ConnectPoint outport, Ethernet payload, int sid) {
155
156         IPv4 ipPacket = (IPv4) payload.getPayload();
157         Ip4Address destIpAddress = Ip4Address.valueOf(ipPacket.getDestinationAddress());
158
159         if (sid == -1 || config.getSegmentId(payload.getDestinationMAC()) == sid ||
160                 config.inSameSubnet(outport.deviceId(), destIpAddress)) {
161             TrafficTreatment treatment = DefaultTrafficTreatment.builder().
162                     setOutput(outport.port()).build();
163             OutboundPacket packet = new DefaultOutboundPacket(outport.deviceId(),
164                     treatment, ByteBuffer.wrap(payload.serialize()));
165             srManager.packetService.emit(packet);
166         } else {
167             log.warn("Send a MPLS packet as a ICMP response");
168             TrafficTreatment treatment = DefaultTrafficTreatment.builder()
169                     .setOutput(outport.port())
170                     .build();
171
172             payload.setEtherType(Ethernet.MPLS_UNICAST);
173             MPLS mplsPkt = new MPLS();
174             mplsPkt.setLabel(sid);
175             mplsPkt.setTtl(((IPv4) payload.getPayload()).getTtl());
176             mplsPkt.setPayload(payload.getPayload());
177             payload.setPayload(mplsPkt);
178
179             OutboundPacket packet = new DefaultOutboundPacket(outport.deviceId(),
180                     treatment, ByteBuffer.wrap(payload.serialize()));
181
182             srManager.packetService.emit(packet);
183         }
184     }
185
186
187 }