blob: 944f9ee38d0d2a59f01899e2a31fe1a8f07102af [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
alshabibc4901cd2014-09-05 16:50:40 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
alshabibc4901cd2014-09-05 16:50:40 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska24c849c2014-10-27 09:53:05 -070015 */
16
alshabibc4901cd2014-09-05 16:50:40 -070017package org.onlab.packet;
18
19import java.nio.ByteBuffer;
20
Jian Li5fc14292015-12-04 11:30:46 -080021import static com.google.common.base.MoreObjects.toStringHelper;
Jonathan Hart2a655752015-04-07 16:46:33 -070022import static org.onlab.packet.PacketUtils.*;
23
alshabibc4901cd2014-09-05 16:50:40 -070024/**
25 * Implements ICMP packet format.
alshabibc4901cd2014-09-05 16:50:40 -070026 */
27public class ICMP extends BasePacket {
28 protected byte icmpType;
29 protected byte icmpCode;
30 protected short checksum;
31
sangho5eaf0332015-03-09 15:08:12 -070032 public static final byte TYPE_ECHO_REQUEST = 0x08;
33 public static final byte TYPE_ECHO_REPLY = 0x00;
Ray Milkey13472a12018-09-20 16:25:01 -070034 /**
35 * @deprecated since Peacock (1.15) - Use CODE_ECHO_REPLY instead.
36 */
Daniel Parkd27dcda2018-08-07 01:40:40 +090037 @Deprecated
sangho5eaf0332015-03-09 15:08:12 -070038 public static final byte SUBTYPE_ECHO_REPLY = 0x00;
39
Daniel Parkd27dcda2018-08-07 01:40:40 +090040 public static final byte CODE_ECHO_REPLY = 0x00;
41 public static final byte CODE_ECHO_REQEUST = 0x00;
42
Jonathan Hart2a655752015-04-07 16:46:33 -070043 public static final short ICMP_HEADER_LENGTH = 4;
44
alshabibc4901cd2014-09-05 16:50:40 -070045 /**
46 * @return the icmpType
47 */
48 public byte getIcmpType() {
49 return this.icmpType;
50 }
51
52 /**
Jian Li5fc14292015-12-04 11:30:46 -080053 * @param icmpType to set
Yuta HIGUCHI2281b3f2014-11-04 00:20:48 -080054 * @return this
alshabibc4901cd2014-09-05 16:50:40 -070055 */
56 public ICMP setIcmpType(final byte icmpType) {
57 this.icmpType = icmpType;
58 return this;
59 }
60
61 /**
62 * @return the icmp code
63 */
64 public byte getIcmpCode() {
65 return this.icmpCode;
66 }
67
68 /**
Jian Li5fc14292015-12-04 11:30:46 -080069 * @param icmpCode code to set
Yuta HIGUCHI2281b3f2014-11-04 00:20:48 -080070 * @return this
alshabibc4901cd2014-09-05 16:50:40 -070071 */
72 public ICMP setIcmpCode(final byte icmpCode) {
73 this.icmpCode = icmpCode;
74 return this;
75 }
76
77 /**
78 * @return the checksum
79 */
80 public short getChecksum() {
81 return this.checksum;
82 }
83
84 /**
Jian Li5fc14292015-12-04 11:30:46 -080085 * @param checksum the checksum to set
Yuta HIGUCHI2281b3f2014-11-04 00:20:48 -080086 * @return this
alshabibc4901cd2014-09-05 16:50:40 -070087 */
88 public ICMP setChecksum(final short checksum) {
89 this.checksum = checksum;
90 return this;
91 }
92
93 /**
94 * Serializes the packet. Will compute and set the following fields if they
95 * are set to specific values at the time serialize is called: -checksum : 0
96 * -length : 0
97 */
98 @Override
99 public byte[] serialize() {
100 int length = 4;
101 byte[] payloadData = null;
102 if (this.payload != null) {
103 this.payload.setParent(this);
104 payloadData = this.payload.serialize();
105 length += payloadData.length;
106 }
107
108 final byte[] data = new byte[length];
109 final ByteBuffer bb = ByteBuffer.wrap(data);
110
111 bb.put(this.icmpType);
112 bb.put(this.icmpCode);
113 bb.putShort(this.checksum);
114 if (payloadData != null) {
115 bb.put(payloadData);
116 }
117
118 if (this.parent != null && this.parent instanceof IPv4) {
119 ((IPv4) this.parent).setProtocol(IPv4.PROTOCOL_ICMP);
120 }
121
122 // compute checksum if needed
123 if (this.checksum == 0) {
124 bb.rewind();
125 int accumulation = 0;
126
127 for (int i = 0; i < length / 2; ++i) {
128 accumulation += 0xffff & bb.getShort();
129 }
130 // pad to an even number of shorts
131 if (length % 2 > 0) {
132 accumulation += (bb.get() & 0xff) << 8;
133 }
134
135 accumulation = (accumulation >> 16 & 0xffff)
136 + (accumulation & 0xffff);
137 this.checksum = (short) (~accumulation & 0xffff);
138 bb.putShort(2, this.checksum);
139 }
140 return data;
141 }
142
143 /*
144 * (non-Javadoc)
145 *
146 * @see java.lang.Object#hashCode()
147 */
148 @Override
149 public int hashCode() {
150 final int prime = 5807;
151 int result = super.hashCode();
152 result = prime * result + this.icmpType;
153 result = prime * result + this.icmpCode;
154 result = prime * result + this.checksum;
155 return result;
156 }
157
158 /*
159 * (non-Javadoc)
160 *
161 * @see java.lang.Object#equals(java.lang.Object)
162 */
163 @Override
164 public boolean equals(final Object obj) {
165 if (this == obj) {
166 return true;
167 }
168 if (!super.equals(obj)) {
169 return false;
170 }
171 if (!(obj instanceof ICMP)) {
172 return false;
173 }
174 final ICMP other = (ICMP) obj;
175 if (this.icmpType != other.icmpType) {
176 return false;
177 }
178 if (this.icmpCode != other.icmpCode) {
179 return false;
180 }
181 if (this.checksum != other.checksum) {
182 return false;
183 }
184 return true;
185 }
186
Jonathan Hart2a655752015-04-07 16:46:33 -0700187 /**
188 * Deserializer function for ICMP packets.
189 *
190 * @return deserializer function
191 */
192 public static Deserializer<ICMP> deserializer() {
193 return (data, offset, length) -> {
194 checkInput(data, offset, length, ICMP_HEADER_LENGTH);
alshabibc4901cd2014-09-05 16:50:40 -0700195
Jonathan Hart2a655752015-04-07 16:46:33 -0700196 ICMP icmp = new ICMP();
197
198 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
199 icmp.icmpType = bb.get();
200 icmp.icmpCode = bb.get();
201 icmp.checksum = bb.getShort();
202
Daniel Parkd27dcda2018-08-07 01:40:40 +0900203 switch (icmp.icmpType) {
204 case ICMP.TYPE_ECHO_REQUEST:
205 case ICMP.TYPE_ECHO_REPLY:
206 Deserializer<ICMPEcho> deserializer = ICMPEcho.deserializer();
207 icmp.payload = deserializer.deserialize(data, bb.position(), ICMPEcho.ICMP_ECHO_HEADER_LENGTH);
208 bb.position(bb.position() + ICMPEcho.ICMP_ECHO_HEADER_LENGTH);
209 icmp.payload.setPayload(Data.deserializer().deserialize(
210 data,
211 bb.position(),
212 bb.limit()
213 - bb.position()
214 ));
215 break;
216 default:
217 icmp.payload = Data.deserializer()
218 .deserialize(data, bb.position(), bb.limit()
219 - bb.position());
220 break;
221 }
222
223
Jonathan Hart2a655752015-04-07 16:46:33 -0700224 icmp.payload.setParent(icmp);
225 return icmp;
226 };
alshabibc4901cd2014-09-05 16:50:40 -0700227 }
Jian Li5fc14292015-12-04 11:30:46 -0800228
229 @Override
230 public String toString() {
231 return toStringHelper(getClass())
232 .add("icmpType", Byte.toString(icmpType))
233 .add("icmpCode", Byte.toString(icmpCode))
234 .add("checksum", Short.toString(checksum))
235 .toString();
236 }
Pier Ventre78e73f62016-12-02 19:59:28 -0800237
238 /**
239 * Builds an ICMP reply using the supplied ICMP request.
240 *
241 * @param ethRequest the Ethernet packet containing the ICMP ECHO request
242 * @return the Ethernet packet containing the ICMP ECHO reply
243 */
244 public static Ethernet buildIcmpReply(Ethernet ethRequest) {
245
246 if (ethRequest.getEtherType() != Ethernet.TYPE_IPV4) {
247 return null;
248 }
249
250 IPv4 ipRequest = (IPv4) ethRequest.getPayload();
251
252 if (ipRequest.getProtocol() != IPv4.PROTOCOL_ICMP) {
253 return null;
254 }
255
256 Ethernet ethReply = new Ethernet();
257 IPv4 ipReply = new IPv4();
258
259 int destAddress = ipRequest.getDestinationAddress();
260 ipReply.setDestinationAddress(ipRequest.getSourceAddress());
261 ipReply.setSourceAddress(destAddress);
262 ipReply.setTtl((byte) 64);
Charles Chan83439a32017-10-24 15:21:16 -0700263 ipReply.setDiffServ(ipRequest.getDiffServ());
Pier Ventre78e73f62016-12-02 19:59:28 -0800264 ipReply.setChecksum((short) 0);
265 ipReply.setProtocol(IPv4.PROTOCOL_ICMP);
266
267 ICMP icmpRequest = (ICMP) ipRequest.getPayload();
268 ICMP icmpReply = new ICMP();
269
270 icmpReply.setPayload(icmpRequest.getPayload());
271 icmpReply.setIcmpType(ICMP.TYPE_ECHO_REPLY);
Ray Milkey13472a12018-09-20 16:25:01 -0700272 icmpReply.setIcmpCode(ICMP.CODE_ECHO_REPLY);
Pier Ventre78e73f62016-12-02 19:59:28 -0800273 icmpReply.setChecksum((short) 0);
274 ipReply.setPayload(icmpReply);
275
276 ethReply.setEtherType(Ethernet.TYPE_IPV4);
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700277 ethReply.setQinQVID(ethRequest.getQinQVID());
278 ethReply.setQinQTPID(ethRequest.getQinQTPID());
Pier Ventre78e73f62016-12-02 19:59:28 -0800279 ethReply.setVlanID(ethRequest.getVlanID());
280 ethReply.setDestinationMACAddress(ethRequest.getSourceMACAddress());
281 ethReply.setSourceMACAddress(ethRequest.getDestinationMACAddress());
282 ethReply.setPayload(ipReply);
283
284
285 return ethReply;
286 }
alshabibc4901cd2014-09-05 16:50:40 -0700287}