f65f03e015ffa231bc71cf10896cb8eae09d08fc
[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.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import java.nio.ByteBuffer;
35 import java.util.List;
36
37 import static com.google.common.base.Preconditions.checkNotNull;
38
39 public class IcmpHandler {
40
41     private static Logger log = LoggerFactory.getLogger(IcmpHandler.class);
42     private SegmentRoutingManager srManager;
43     private DeviceConfiguration config;
44
45     /**
46      * Creates an IcmpHandler object.
47      *
48      * @param srManager SegmentRoutingManager object
49      */
50     public IcmpHandler(SegmentRoutingManager srManager) {
51         this.srManager = srManager;
52         this.config = checkNotNull(srManager.deviceConfiguration);
53     }
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      *
62      * @param pkt inbound packet
63      */
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());
73         List<Ip4Address> gatewayIpAddresses = config.getSubnetGatewayIps(deviceId);
74         Ip4Address routerIp = config.getRouterIp(deviceId);
75         IpPrefix routerIpPrefix = IpPrefix.valueOf(routerIp, IpPrefix.MAX_INET_MASK_LENGTH);
76         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) ||
81                         gatewayIpAddresses.contains(destinationAddress))) {
82             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
113         ICMP icmpReply = new ICMP();
114         icmpReply.setPayload(((ICMP) icmpRequestIpv4.getPayload()).getPayload());
115         icmpReply.setIcmpType(ICMP.TYPE_ECHO_REPLY);
116         icmpReply.setIcmpCode(ICMP.SUBTYPE_ECHO_REPLY);
117         icmpReply.setChecksum((short) 0);
118         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());
127         Ip4Address destRouterAddress = config.getRouterIpAddressForASubnetHost(destIpAddress);
128         int sid = config.getSegmentId(destRouterAddress);
129         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
143         if (sid == -1 || config.getSegmentId(payload.getDestinationMAC()) == sid ||
144                 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     }
169
170
171 }