blob: f503667ec36d78a23745c5bca9b90c38a5cd8aa9 [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;
Daniel Parkd27dcda2018-08-07 01:40:40 +090034 //Uses CODE_ECHO_REPLY instead.
35 @Deprecated
sangho5eaf0332015-03-09 15:08:12 -070036 public static final byte SUBTYPE_ECHO_REPLY = 0x00;
37
Daniel Parkd27dcda2018-08-07 01:40:40 +090038 public static final byte CODE_ECHO_REPLY = 0x00;
39 public static final byte CODE_ECHO_REQEUST = 0x00;
40
Jonathan Hart2a655752015-04-07 16:46:33 -070041 public static final short ICMP_HEADER_LENGTH = 4;
42
alshabibc4901cd2014-09-05 16:50:40 -070043 /**
44 * @return the icmpType
45 */
46 public byte getIcmpType() {
47 return this.icmpType;
48 }
49
50 /**
Jian Li5fc14292015-12-04 11:30:46 -080051 * @param icmpType to set
Yuta HIGUCHI2281b3f2014-11-04 00:20:48 -080052 * @return this
alshabibc4901cd2014-09-05 16:50:40 -070053 */
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 Li5fc14292015-12-04 11:30:46 -080067 * @param icmpCode code to set
Yuta HIGUCHI2281b3f2014-11-04 00:20:48 -080068 * @return this
alshabibc4901cd2014-09-05 16:50:40 -070069 */
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 Li5fc14292015-12-04 11:30:46 -080083 * @param checksum the checksum to set
Yuta HIGUCHI2281b3f2014-11-04 00:20:48 -080084 * @return this
alshabibc4901cd2014-09-05 16:50:40 -070085 */
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 Hart2a655752015-04-07 16:46:33 -0700185 /**
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);
alshabibc4901cd2014-09-05 16:50:40 -0700193
Jonathan Hart2a655752015-04-07 16:46:33 -0700194 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 Parkd27dcda2018-08-07 01:40:40 +0900201 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 Hart2a655752015-04-07 16:46:33 -0700222 icmp.payload.setParent(icmp);
223 return icmp;
224 };
alshabibc4901cd2014-09-05 16:50:40 -0700225 }
Jian Li5fc14292015-12-04 11:30:46 -0800226
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 Ventre78e73f62016-12-02 19:59:28 -0800235
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 Chan83439a32017-10-24 15:21:16 -0700261 ipReply.setDiffServ(ipRequest.getDiffServ());
Pier Ventre78e73f62016-12-02 19:59:28 -0800262 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 Hyun800d9d02018-04-09 09:40:50 -0700275 ethReply.setQinQVID(ethRequest.getQinQVID());
276 ethReply.setQinQTPID(ethRequest.getQinQTPID());
Pier Ventre78e73f62016-12-02 19:59:28 -0800277 ethReply.setVlanID(ethRequest.getVlanID());
278 ethReply.setDestinationMACAddress(ethRequest.getSourceMACAddress());
279 ethReply.setSourceMACAddress(ethRequest.getDestinationMACAddress());
280 ethReply.setPayload(ipReply);
281
282
283 return ethReply;
284 }
alshabibc4901cd2014-09-05 16:50:40 -0700285}