sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 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.bgprouter; |
| 17 | |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 18 | import org.onlab.packet.Ethernet; |
| 19 | import org.onlab.packet.ICMP; |
| 20 | import org.onlab.packet.IPv4; |
| 21 | import org.onlab.packet.IpAddress; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 22 | import org.onosproject.incubator.net.intf.Interface; |
| 23 | import org.onosproject.incubator.net.intf.InterfaceService; |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 24 | import org.onosproject.net.ConnectPoint; |
| 25 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 26 | import org.onosproject.net.flow.TrafficTreatment; |
| 27 | import org.onosproject.net.host.InterfaceIpAddress; |
| 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.net.packet.PacketContext; |
| 32 | import org.onosproject.net.packet.PacketProcessor; |
| 33 | import org.onosproject.net.packet.PacketService; |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
| 36 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 37 | import java.nio.ByteBuffer; |
| 38 | |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 39 | public class IcmpHandler { |
| 40 | |
| 41 | private static final Logger log = LoggerFactory.getLogger(IcmpHandler.class); |
| 42 | |
| 43 | private final PacketService packetService; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 44 | private final InterfaceService interfaceService; |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 45 | |
| 46 | private final IcmpProcessor processor = new IcmpProcessor(); |
| 47 | |
| 48 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 49 | public IcmpHandler(InterfaceService interfaceService, |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 50 | PacketService packetService) { |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 51 | this.interfaceService = interfaceService; |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 52 | this.packetService = packetService; |
| 53 | } |
| 54 | |
| 55 | public void start() { |
Brian O'Connor | 3b78326 | 2015-07-29 17:49:24 -0700 | [diff] [blame] | 56 | packetService.addProcessor(processor, PacketProcessor.director(4)); |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 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()); |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 70 | Interface targetInterface = interfaceService.getMatchingInterface(destIpAddress); |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 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) { |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 86 | sendIcmpResponse(ethernet, connectPoint); |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 90 | private void sendIcmpResponse(Ethernet icmpRequest, ConnectPoint outport) { |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 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 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 103 | ICMP icmpReply = new ICMP(); |
Saurav Das | 428ebc9 | 2015-09-30 10:00:49 -0700 | [diff] [blame] | 104 | icmpReply.setPayload(((ICMP) icmpRequestIpv4.getPayload()).getPayload()); |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 105 | 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 | } |