Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 17 | package org.onlab.packet; |
| 18 | |
| 19 | import java.nio.ByteBuffer; |
| 20 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 21 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 22 | import static org.onlab.packet.PacketUtils.*; |
| 23 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 24 | /** |
| 25 | * Implements ICMP packet format. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 26 | */ |
| 27 | public class ICMP extends BasePacket { |
| 28 | protected byte icmpType; |
| 29 | protected byte icmpCode; |
| 30 | protected short checksum; |
| 31 | |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 32 | public static final byte TYPE_ECHO_REQUEST = 0x08; |
| 33 | public static final byte TYPE_ECHO_REPLY = 0x00; |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 34 | |
Daniel Park | d27dcda | 2018-08-07 01:40:40 +0900 | [diff] [blame] | 35 | public static final byte CODE_ECHO_REPLY = 0x00; |
| 36 | public static final byte CODE_ECHO_REQEUST = 0x00; |
| 37 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 38 | public static final short ICMP_HEADER_LENGTH = 4; |
| 39 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 40 | /** |
| 41 | * @return the icmpType |
| 42 | */ |
| 43 | public byte getIcmpType() { |
| 44 | return this.icmpType; |
| 45 | } |
| 46 | |
| 47 | /** |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 48 | * @param icmpType to set |
Yuta HIGUCHI | 2281b3f | 2014-11-04 00:20:48 -0800 | [diff] [blame] | 49 | * @return this |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 50 | */ |
| 51 | public ICMP setIcmpType(final byte icmpType) { |
| 52 | this.icmpType = icmpType; |
| 53 | return this; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @return the icmp code |
| 58 | */ |
| 59 | public byte getIcmpCode() { |
| 60 | return this.icmpCode; |
| 61 | } |
| 62 | |
| 63 | /** |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 64 | * @param icmpCode code to set |
Yuta HIGUCHI | 2281b3f | 2014-11-04 00:20:48 -0800 | [diff] [blame] | 65 | * @return this |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 66 | */ |
| 67 | public ICMP setIcmpCode(final byte icmpCode) { |
| 68 | this.icmpCode = icmpCode; |
| 69 | return this; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @return the checksum |
| 74 | */ |
| 75 | public short getChecksum() { |
| 76 | return this.checksum; |
| 77 | } |
| 78 | |
| 79 | /** |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 80 | * @param checksum the checksum to set |
Yuta HIGUCHI | 2281b3f | 2014-11-04 00:20:48 -0800 | [diff] [blame] | 81 | * @return this |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 82 | */ |
| 83 | public ICMP setChecksum(final short checksum) { |
| 84 | this.checksum = checksum; |
| 85 | return this; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Serializes the packet. Will compute and set the following fields if they |
| 90 | * are set to specific values at the time serialize is called: -checksum : 0 |
| 91 | * -length : 0 |
| 92 | */ |
| 93 | @Override |
| 94 | public byte[] serialize() { |
| 95 | int length = 4; |
| 96 | byte[] payloadData = null; |
| 97 | if (this.payload != null) { |
| 98 | this.payload.setParent(this); |
| 99 | payloadData = this.payload.serialize(); |
| 100 | length += payloadData.length; |
| 101 | } |
| 102 | |
| 103 | final byte[] data = new byte[length]; |
| 104 | final ByteBuffer bb = ByteBuffer.wrap(data); |
| 105 | |
| 106 | bb.put(this.icmpType); |
| 107 | bb.put(this.icmpCode); |
| 108 | bb.putShort(this.checksum); |
| 109 | if (payloadData != null) { |
| 110 | bb.put(payloadData); |
| 111 | } |
| 112 | |
| 113 | if (this.parent != null && this.parent instanceof IPv4) { |
| 114 | ((IPv4) this.parent).setProtocol(IPv4.PROTOCOL_ICMP); |
| 115 | } |
| 116 | |
| 117 | // compute checksum if needed |
| 118 | if (this.checksum == 0) { |
| 119 | bb.rewind(); |
| 120 | int accumulation = 0; |
| 121 | |
| 122 | for (int i = 0; i < length / 2; ++i) { |
| 123 | accumulation += 0xffff & bb.getShort(); |
| 124 | } |
| 125 | // pad to an even number of shorts |
| 126 | if (length % 2 > 0) { |
| 127 | accumulation += (bb.get() & 0xff) << 8; |
| 128 | } |
| 129 | |
| 130 | accumulation = (accumulation >> 16 & 0xffff) |
| 131 | + (accumulation & 0xffff); |
| 132 | this.checksum = (short) (~accumulation & 0xffff); |
| 133 | bb.putShort(2, this.checksum); |
| 134 | } |
| 135 | return data; |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * (non-Javadoc) |
| 140 | * |
| 141 | * @see java.lang.Object#hashCode() |
| 142 | */ |
| 143 | @Override |
| 144 | public int hashCode() { |
| 145 | final int prime = 5807; |
| 146 | int result = super.hashCode(); |
| 147 | result = prime * result + this.icmpType; |
| 148 | result = prime * result + this.icmpCode; |
| 149 | result = prime * result + this.checksum; |
| 150 | return result; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * (non-Javadoc) |
| 155 | * |
| 156 | * @see java.lang.Object#equals(java.lang.Object) |
| 157 | */ |
| 158 | @Override |
| 159 | public boolean equals(final Object obj) { |
| 160 | if (this == obj) { |
| 161 | return true; |
| 162 | } |
| 163 | if (!super.equals(obj)) { |
| 164 | return false; |
| 165 | } |
| 166 | if (!(obj instanceof ICMP)) { |
| 167 | return false; |
| 168 | } |
| 169 | final ICMP other = (ICMP) obj; |
| 170 | if (this.icmpType != other.icmpType) { |
| 171 | return false; |
| 172 | } |
| 173 | if (this.icmpCode != other.icmpCode) { |
| 174 | return false; |
| 175 | } |
| 176 | if (this.checksum != other.checksum) { |
| 177 | return false; |
| 178 | } |
| 179 | return true; |
| 180 | } |
| 181 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 182 | /** |
| 183 | * Deserializer function for ICMP packets. |
| 184 | * |
| 185 | * @return deserializer function |
| 186 | */ |
| 187 | public static Deserializer<ICMP> deserializer() { |
| 188 | return (data, offset, length) -> { |
| 189 | checkInput(data, offset, length, ICMP_HEADER_LENGTH); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 190 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 191 | ICMP icmp = new ICMP(); |
| 192 | |
| 193 | final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 194 | icmp.icmpType = bb.get(); |
| 195 | icmp.icmpCode = bb.get(); |
| 196 | icmp.checksum = bb.getShort(); |
| 197 | |
Daniel Park | d27dcda | 2018-08-07 01:40:40 +0900 | [diff] [blame] | 198 | switch (icmp.icmpType) { |
| 199 | case ICMP.TYPE_ECHO_REQUEST: |
| 200 | case ICMP.TYPE_ECHO_REPLY: |
| 201 | Deserializer<ICMPEcho> deserializer = ICMPEcho.deserializer(); |
| 202 | icmp.payload = deserializer.deserialize(data, bb.position(), ICMPEcho.ICMP_ECHO_HEADER_LENGTH); |
| 203 | bb.position(bb.position() + ICMPEcho.ICMP_ECHO_HEADER_LENGTH); |
| 204 | icmp.payload.setPayload(Data.deserializer().deserialize( |
| 205 | data, |
| 206 | bb.position(), |
| 207 | bb.limit() |
| 208 | - bb.position() |
| 209 | )); |
| 210 | break; |
| 211 | default: |
| 212 | icmp.payload = Data.deserializer() |
| 213 | .deserialize(data, bb.position(), bb.limit() |
| 214 | - bb.position()); |
| 215 | break; |
| 216 | } |
| 217 | |
| 218 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 219 | icmp.payload.setParent(icmp); |
| 220 | return icmp; |
| 221 | }; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 222 | } |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 223 | |
| 224 | @Override |
| 225 | public String toString() { |
| 226 | return toStringHelper(getClass()) |
| 227 | .add("icmpType", Byte.toString(icmpType)) |
| 228 | .add("icmpCode", Byte.toString(icmpCode)) |
| 229 | .add("checksum", Short.toString(checksum)) |
| 230 | .toString(); |
| 231 | } |
Pier Ventre | 78e73f6 | 2016-12-02 19:59:28 -0800 | [diff] [blame] | 232 | |
| 233 | /** |
| 234 | * Builds an ICMP reply using the supplied ICMP request. |
| 235 | * |
| 236 | * @param ethRequest the Ethernet packet containing the ICMP ECHO request |
| 237 | * @return the Ethernet packet containing the ICMP ECHO reply |
| 238 | */ |
| 239 | public static Ethernet buildIcmpReply(Ethernet ethRequest) { |
| 240 | |
| 241 | if (ethRequest.getEtherType() != Ethernet.TYPE_IPV4) { |
| 242 | return null; |
| 243 | } |
| 244 | |
| 245 | IPv4 ipRequest = (IPv4) ethRequest.getPayload(); |
| 246 | |
| 247 | if (ipRequest.getProtocol() != IPv4.PROTOCOL_ICMP) { |
| 248 | return null; |
| 249 | } |
| 250 | |
| 251 | Ethernet ethReply = new Ethernet(); |
| 252 | IPv4 ipReply = new IPv4(); |
| 253 | |
| 254 | int destAddress = ipRequest.getDestinationAddress(); |
| 255 | ipReply.setDestinationAddress(ipRequest.getSourceAddress()); |
| 256 | ipReply.setSourceAddress(destAddress); |
| 257 | ipReply.setTtl((byte) 64); |
Charles Chan | 83439a3 | 2017-10-24 15:21:16 -0700 | [diff] [blame] | 258 | ipReply.setDiffServ(ipRequest.getDiffServ()); |
Pier Ventre | 78e73f6 | 2016-12-02 19:59:28 -0800 | [diff] [blame] | 259 | ipReply.setChecksum((short) 0); |
| 260 | ipReply.setProtocol(IPv4.PROTOCOL_ICMP); |
| 261 | |
| 262 | ICMP icmpRequest = (ICMP) ipRequest.getPayload(); |
| 263 | ICMP icmpReply = new ICMP(); |
| 264 | |
| 265 | icmpReply.setPayload(icmpRequest.getPayload()); |
| 266 | icmpReply.setIcmpType(ICMP.TYPE_ECHO_REPLY); |
Ray Milkey | 13472a1 | 2018-09-20 16:25:01 -0700 | [diff] [blame] | 267 | icmpReply.setIcmpCode(ICMP.CODE_ECHO_REPLY); |
Pier Ventre | 78e73f6 | 2016-12-02 19:59:28 -0800 | [diff] [blame] | 268 | icmpReply.setChecksum((short) 0); |
| 269 | ipReply.setPayload(icmpReply); |
| 270 | |
| 271 | ethReply.setEtherType(Ethernet.TYPE_IPV4); |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 272 | ethReply.setQinQVID(ethRequest.getQinQVID()); |
| 273 | ethReply.setQinQTPID(ethRequest.getQinQTPID()); |
Pier Ventre | 78e73f6 | 2016-12-02 19:59:28 -0800 | [diff] [blame] | 274 | ethReply.setVlanID(ethRequest.getVlanID()); |
| 275 | ethReply.setDestinationMACAddress(ethRequest.getSourceMACAddress()); |
| 276 | ethReply.setSourceMACAddress(ethRequest.getDestinationMACAddress()); |
| 277 | ethReply.setPayload(ipReply); |
| 278 | |
| 279 | |
| 280 | return ethReply; |
| 281 | } |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 282 | } |