blob: 1af3fd9a447c640160454b3d10507587e418ccf0 [file] [log] [blame]
sangho5eaf0332015-03-09 15:08:12 -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.bgprouter;
17
18import java.nio.ByteBuffer;
19
20import org.onlab.packet.Ethernet;
21import org.onlab.packet.ICMP;
22import org.onlab.packet.IPv4;
23import org.onlab.packet.IpAddress;
24import 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;
34import org.onosproject.routing.config.Interface;
35import org.onosproject.routing.config.RoutingConfigurationService;
36import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
38
39public class IcmpHandler {
40
41 private static final Logger log = LoggerFactory.getLogger(IcmpHandler.class);
42
43 private final PacketService packetService;
44 private final RoutingConfigurationService configService;
45
46 private final IcmpProcessor processor = new IcmpProcessor();
47
48
49 public IcmpHandler(RoutingConfigurationService configService,
50 PacketService packetService) {
51 this.configService = configService;
52 this.packetService = packetService;
53 }
54
55 public void start() {
56 packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 4);
57 }
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());
70 Interface targetInterface = configService.getMatchingInterface(destIpAddress);
71
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) {
86 sendICMPResponse(ethernet, connectPoint);
87 }
88 }
89
90 private void sendICMPResponse(Ethernet icmpRequest, ConnectPoint outport) {
91
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
103 ICMP icmpReply = (ICMP) icmpRequestIpv4.getPayload().clone();
104 icmpReply.setIcmpType(ICMP.TYPE_ECHO_REPLY);
105 icmpReply.setIcmpCode(ICMP.SUBTYPE_ECHO_REPLY);
106 icmpReply.setChecksum((short) 0);
107
108 icmpReplyIpv4.setPayload(icmpReply);
109
110 icmpReplyEth.setPayload(icmpReplyIpv4);
111 icmpReplyEth.setEtherType(Ethernet.TYPE_IPV4);
112 icmpReplyEth.setDestinationMACAddress(icmpRequest.getSourceMACAddress());
113 icmpReplyEth.setSourceMACAddress(icmpRequest.getDestinationMACAddress());
114 icmpReplyEth.setVlanID(icmpRequest.getVlanID());
115
116 sendPacketOut(outport, icmpReplyEth);
117
118 }
119
120 private void sendPacketOut(ConnectPoint outport, Ethernet payload) {
121 TrafficTreatment treatment = DefaultTrafficTreatment.builder().
122 setOutput(outport.port()).build();
123 OutboundPacket packet = new DefaultOutboundPacket(outport.deviceId(),
124 treatment, ByteBuffer.wrap(payload.serialize()));
125 packetService.emit(packet);
126 }
127
128 /**
129 * Packet processor responsible receiving and filtering ICMP packets.
130 */
131 private class IcmpProcessor implements PacketProcessor {
132
133 @Override
134 public void process(PacketContext context) {
135 // Stop processing if the packet has been handled, since we
136 // can't do any more to it.
137
138 if (context.isHandled()) {
139 return;
140 }
141
142 Ethernet packet = context.inPacket().parsed();
143
144 if (packet == null) {
145 return;
146 }
147
148 if (packet.getEtherType() == Ethernet.TYPE_IPV4) {
149 IPv4 ipv4Packet = (IPv4) packet.getPayload();
150 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
151 processPacketIn(context.inPacket());
152 }
153 }
154 }
155 }
156
157}