blob: 14196a281a902e23d65642be3951bc34e9a79eea [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;
sangho5eaf0332015-03-09 15:08:12 -070034
Daniel Parkd27dcda2018-08-07 01:40:40 +090035 public static final byte CODE_ECHO_REPLY = 0x00;
36 public static final byte CODE_ECHO_REQEUST = 0x00;
37
Jonathan Hart2a655752015-04-07 16:46:33 -070038 public static final short ICMP_HEADER_LENGTH = 4;
39
alshabibc4901cd2014-09-05 16:50:40 -070040 /**
41 * @return the icmpType
42 */
43 public byte getIcmpType() {
44 return this.icmpType;
45 }
46
47 /**
Jian Li5fc14292015-12-04 11:30:46 -080048 * @param icmpType to set
Yuta HIGUCHI2281b3f2014-11-04 00:20:48 -080049 * @return this
alshabibc4901cd2014-09-05 16:50:40 -070050 */
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 Li5fc14292015-12-04 11:30:46 -080064 * @param icmpCode code to set
Yuta HIGUCHI2281b3f2014-11-04 00:20:48 -080065 * @return this
alshabibc4901cd2014-09-05 16:50:40 -070066 */
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 Li5fc14292015-12-04 11:30:46 -080080 * @param checksum the checksum to set
Yuta HIGUCHI2281b3f2014-11-04 00:20:48 -080081 * @return this
alshabibc4901cd2014-09-05 16:50:40 -070082 */
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 Hart2a655752015-04-07 16:46:33 -0700182 /**
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);
alshabibc4901cd2014-09-05 16:50:40 -0700190
Jonathan Hart2a655752015-04-07 16:46:33 -0700191 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 Parkd27dcda2018-08-07 01:40:40 +0900198 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 Hart2a655752015-04-07 16:46:33 -0700219 icmp.payload.setParent(icmp);
220 return icmp;
221 };
alshabibc4901cd2014-09-05 16:50:40 -0700222 }
Jian Li5fc14292015-12-04 11:30:46 -0800223
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 Ventre78e73f62016-12-02 19:59:28 -0800232
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 Chan83439a32017-10-24 15:21:16 -0700258 ipReply.setDiffServ(ipRequest.getDiffServ());
Pier Ventre78e73f62016-12-02 19:59:28 -0800259 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 Milkey13472a12018-09-20 16:25:01 -0700267 icmpReply.setIcmpCode(ICMP.CODE_ECHO_REPLY);
Pier Ventre78e73f62016-12-02 19:59:28 -0800268 icmpReply.setChecksum((short) 0);
269 ipReply.setPayload(icmpReply);
270
271 ethReply.setEtherType(Ethernet.TYPE_IPV4);
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700272 ethReply.setQinQVID(ethRequest.getQinQVID());
273 ethReply.setQinQTPID(ethRequest.getQinQTPID());
Pier Ventre78e73f62016-12-02 19:59:28 -0800274 ethReply.setVlanID(ethRequest.getVlanID());
275 ethReply.setDestinationMACAddress(ethRequest.getSourceMACAddress());
276 ethReply.setSourceMACAddress(ethRequest.getDestinationMACAddress());
277 ethReply.setPayload(ipReply);
278
279
280 return ethReply;
281 }
alshabibc4901cd2014-09-05 16:50:40 -0700282}