blob: 99c6fc1713369b4a5bfcc243fcf7a9dc6e13d63c [file] [log] [blame]
sangho5eaf0332015-03-09 15:08:12 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
sangho5eaf0332015-03-09 15:08:12 -07003 *
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.bgprouter;
17
sangho5eaf0332015-03-09 15:08:12 -070018import org.onlab.packet.Ethernet;
19import org.onlab.packet.ICMP;
20import org.onlab.packet.IPv4;
21import org.onlab.packet.IpAddress;
Jonathan Hart4cb39882015-08-12 23:50:55 -040022import org.onosproject.incubator.net.intf.Interface;
23import org.onosproject.incubator.net.intf.InterfaceService;
sangho5eaf0332015-03-09 15:08:12 -070024import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.flow.DefaultTrafficTreatment;
26import org.onosproject.net.flow.TrafficTreatment;
27import org.onosproject.net.host.InterfaceIpAddress;
28import org.onosproject.net.packet.DefaultOutboundPacket;
29import org.onosproject.net.packet.InboundPacket;
30import org.onosproject.net.packet.OutboundPacket;
31import org.onosproject.net.packet.PacketContext;
32import org.onosproject.net.packet.PacketProcessor;
33import org.onosproject.net.packet.PacketService;
sangho5eaf0332015-03-09 15:08:12 -070034import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
36
Jonathan Hart2a655752015-04-07 16:46:33 -070037import java.nio.ByteBuffer;
38
sangho5eaf0332015-03-09 15:08:12 -070039public class IcmpHandler {
40
41 private static final Logger log = LoggerFactory.getLogger(IcmpHandler.class);
42
43 private final PacketService packetService;
Jonathan Hart4cb39882015-08-12 23:50:55 -040044 private final InterfaceService interfaceService;
sangho5eaf0332015-03-09 15:08:12 -070045
46 private final IcmpProcessor processor = new IcmpProcessor();
47
48
Jonathan Hart4cb39882015-08-12 23:50:55 -040049 public IcmpHandler(InterfaceService interfaceService,
sangho5eaf0332015-03-09 15:08:12 -070050 PacketService packetService) {
Jonathan Hart4cb39882015-08-12 23:50:55 -040051 this.interfaceService = interfaceService;
sangho5eaf0332015-03-09 15:08:12 -070052 this.packetService = packetService;
53 }
54
55 public void start() {
Brian O'Connor3b783262015-07-29 17:49:24 -070056 packetService.addProcessor(processor, PacketProcessor.director(4));
sangho5eaf0332015-03-09 15:08:12 -070057 }
58
59 public void stop() {
60 packetService.removeProcessor(processor);
61 }
62
63 private void processPacketIn(InboundPacket pkt) {
64
65 boolean ipMatches = false;
66 Ethernet ethernet = pkt.parsed();
67 IPv4 ipv4 = (IPv4) ethernet.getPayload();
68 ConnectPoint connectPoint = pkt.receivedFrom();
69 IpAddress destIpAddress = IpAddress.valueOf(ipv4.getDestinationAddress());
Jonathan Hart4cb39882015-08-12 23:50:55 -040070 Interface targetInterface = interfaceService.getMatchingInterface(destIpAddress);
sangho5eaf0332015-03-09 15:08:12 -070071
72 if (targetInterface == null) {
73 log.trace("No matching interface for {}", destIpAddress);
74 return;
75 }
76
77 for (InterfaceIpAddress interfaceIpAddress: targetInterface.ipAddresses()) {
78 if (interfaceIpAddress.ipAddress().equals(destIpAddress)) {
79 ipMatches = true;
80 break;
81 }
82 }
83
84 if (((ICMP) ipv4.getPayload()).getIcmpType() == ICMP.TYPE_ECHO_REQUEST &&
85 ipMatches) {
Jonathan Hartd9df7bd2015-11-10 17:10:25 -080086 sendIcmpResponse(ethernet, connectPoint);
sangho5eaf0332015-03-09 15:08:12 -070087 }
88 }
89
Jonathan Hartd9df7bd2015-11-10 17:10:25 -080090 private void sendIcmpResponse(Ethernet icmpRequest, ConnectPoint outport) {
sangho5eaf0332015-03-09 15:08:12 -070091
92 Ethernet icmpReplyEth = new Ethernet();
93
94 IPv4 icmpRequestIpv4 = (IPv4) icmpRequest.getPayload();
95 IPv4 icmpReplyIpv4 = new IPv4();
96
97 int destAddress = icmpRequestIpv4.getDestinationAddress();
98 icmpReplyIpv4.setDestinationAddress(icmpRequestIpv4.getSourceAddress());
99 icmpReplyIpv4.setSourceAddress(destAddress);
100 icmpReplyIpv4.setTtl((byte) 64);
101 icmpReplyIpv4.setChecksum((short) 0);
102
Jonathan Hart2a655752015-04-07 16:46:33 -0700103 ICMP icmpReply = new ICMP();
Saurav Das428ebc92015-09-30 10:00:49 -0700104 icmpReply.setPayload(((ICMP) icmpRequestIpv4.getPayload()).getPayload());
sangho5eaf0332015-03-09 15:08:12 -0700105 icmpReply.setIcmpType(ICMP.TYPE_ECHO_REPLY);
106 icmpReply.setIcmpCode(ICMP.SUBTYPE_ECHO_REPLY);
107 icmpReply.setChecksum((short) 0);
108
109 icmpReplyIpv4.setPayload(icmpReply);
110
111 icmpReplyEth.setPayload(icmpReplyIpv4);
112 icmpReplyEth.setEtherType(Ethernet.TYPE_IPV4);
113 icmpReplyEth.setDestinationMACAddress(icmpRequest.getSourceMACAddress());
114 icmpReplyEth.setSourceMACAddress(icmpRequest.getDestinationMACAddress());
115 icmpReplyEth.setVlanID(icmpRequest.getVlanID());
116
117 sendPacketOut(outport, icmpReplyEth);
118
119 }
120
121 private void sendPacketOut(ConnectPoint outport, Ethernet payload) {
122 TrafficTreatment treatment = DefaultTrafficTreatment.builder().
123 setOutput(outport.port()).build();
124 OutboundPacket packet = new DefaultOutboundPacket(outport.deviceId(),
125 treatment, ByteBuffer.wrap(payload.serialize()));
126 packetService.emit(packet);
127 }
128
129 /**
130 * Packet processor responsible receiving and filtering ICMP packets.
131 */
132 private class IcmpProcessor implements PacketProcessor {
133
134 @Override
135 public void process(PacketContext context) {
136 // Stop processing if the packet has been handled, since we
137 // can't do any more to it.
138
139 if (context.isHandled()) {
140 return;
141 }
142
143 Ethernet packet = context.inPacket().parsed();
144
145 if (packet == null) {
146 return;
147 }
148
149 if (packet.getEtherType() == Ethernet.TYPE_IPV4) {
150 IPv4 ipv4Packet = (IPv4) packet.getPayload();
151 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
152 processPacketIn(context.inPacket());
153 }
154 }
155 }
156 }
157
158}