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