blob: 68fddcb362bb3c26988f43a1a155b45f9f694cf6 [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
17
alshabibc4901cd2014-09-05 16:50:40 -070018package org.onlab.packet;
19
20import java.nio.ByteBuffer;
21
Jian Li5fc14292015-12-04 11:30:46 -080022import static com.google.common.base.MoreObjects.toStringHelper;
Jonathan Hart2a655752015-04-07 16:46:33 -070023import static org.onlab.packet.PacketUtils.*;
24
alshabibc4901cd2014-09-05 16:50:40 -070025/**
26 * Implements ICMP packet format.
alshabibc4901cd2014-09-05 16:50:40 -070027 */
28public class ICMP extends BasePacket {
29 protected byte icmpType;
30 protected byte icmpCode;
31 protected short checksum;
32
sangho5eaf0332015-03-09 15:08:12 -070033 public static final byte TYPE_ECHO_REQUEST = 0x08;
34 public static final byte TYPE_ECHO_REPLY = 0x00;
35 public static final byte SUBTYPE_ECHO_REPLY = 0x00;
36
Jonathan Hart2a655752015-04-07 16:46:33 -070037 public static final short ICMP_HEADER_LENGTH = 4;
38
alshabibc4901cd2014-09-05 16:50:40 -070039 /**
40 * @return the icmpType
41 */
42 public byte getIcmpType() {
43 return this.icmpType;
44 }
45
46 /**
Jian Li5fc14292015-12-04 11:30:46 -080047 * @param icmpType to set
Yuta HIGUCHI2281b3f2014-11-04 00:20:48 -080048 * @return this
alshabibc4901cd2014-09-05 16:50:40 -070049 */
50 public ICMP setIcmpType(final byte icmpType) {
51 this.icmpType = icmpType;
52 return this;
53 }
54
55 /**
56 * @return the icmp code
57 */
58 public byte getIcmpCode() {
59 return this.icmpCode;
60 }
61
62 /**
Jian Li5fc14292015-12-04 11:30:46 -080063 * @param icmpCode code to set
Yuta HIGUCHI2281b3f2014-11-04 00:20:48 -080064 * @return this
alshabibc4901cd2014-09-05 16:50:40 -070065 */
66 public ICMP setIcmpCode(final byte icmpCode) {
67 this.icmpCode = icmpCode;
68 return this;
69 }
70
71 /**
72 * @return the checksum
73 */
74 public short getChecksum() {
75 return this.checksum;
76 }
77
78 /**
Jian Li5fc14292015-12-04 11:30:46 -080079 * @param checksum the checksum to set
Yuta HIGUCHI2281b3f2014-11-04 00:20:48 -080080 * @return this
alshabibc4901cd2014-09-05 16:50:40 -070081 */
82 public ICMP setChecksum(final short checksum) {
83 this.checksum = checksum;
84 return this;
85 }
86
87 /**
88 * Serializes the packet. Will compute and set the following fields if they
89 * are set to specific values at the time serialize is called: -checksum : 0
90 * -length : 0
91 */
92 @Override
93 public byte[] serialize() {
94 int length = 4;
95 byte[] payloadData = null;
96 if (this.payload != null) {
97 this.payload.setParent(this);
98 payloadData = this.payload.serialize();
99 length += payloadData.length;
100 }
101
102 final byte[] data = new byte[length];
103 final ByteBuffer bb = ByteBuffer.wrap(data);
104
105 bb.put(this.icmpType);
106 bb.put(this.icmpCode);
107 bb.putShort(this.checksum);
108 if (payloadData != null) {
109 bb.put(payloadData);
110 }
111
112 if (this.parent != null && this.parent instanceof IPv4) {
113 ((IPv4) this.parent).setProtocol(IPv4.PROTOCOL_ICMP);
114 }
115
116 // compute checksum if needed
117 if (this.checksum == 0) {
118 bb.rewind();
119 int accumulation = 0;
120
121 for (int i = 0; i < length / 2; ++i) {
122 accumulation += 0xffff & bb.getShort();
123 }
124 // pad to an even number of shorts
125 if (length % 2 > 0) {
126 accumulation += (bb.get() & 0xff) << 8;
127 }
128
129 accumulation = (accumulation >> 16 & 0xffff)
130 + (accumulation & 0xffff);
131 this.checksum = (short) (~accumulation & 0xffff);
132 bb.putShort(2, this.checksum);
133 }
134 return data;
135 }
136
137 /*
138 * (non-Javadoc)
139 *
140 * @see java.lang.Object#hashCode()
141 */
142 @Override
143 public int hashCode() {
144 final int prime = 5807;
145 int result = super.hashCode();
146 result = prime * result + this.icmpType;
147 result = prime * result + this.icmpCode;
148 result = prime * result + this.checksum;
149 return result;
150 }
151
152 /*
153 * (non-Javadoc)
154 *
155 * @see java.lang.Object#equals(java.lang.Object)
156 */
157 @Override
158 public boolean equals(final Object obj) {
159 if (this == obj) {
160 return true;
161 }
162 if (!super.equals(obj)) {
163 return false;
164 }
165 if (!(obj instanceof ICMP)) {
166 return false;
167 }
168 final ICMP other = (ICMP) obj;
169 if (this.icmpType != other.icmpType) {
170 return false;
171 }
172 if (this.icmpCode != other.icmpCode) {
173 return false;
174 }
175 if (this.checksum != other.checksum) {
176 return false;
177 }
178 return true;
179 }
180
Jonathan Hart2a655752015-04-07 16:46:33 -0700181 /**
182 * Deserializer function for ICMP packets.
183 *
184 * @return deserializer function
185 */
186 public static Deserializer<ICMP> deserializer() {
187 return (data, offset, length) -> {
188 checkInput(data, offset, length, ICMP_HEADER_LENGTH);
alshabibc4901cd2014-09-05 16:50:40 -0700189
Jonathan Hart2a655752015-04-07 16:46:33 -0700190 ICMP icmp = new ICMP();
191
192 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
193 icmp.icmpType = bb.get();
194 icmp.icmpCode = bb.get();
195 icmp.checksum = bb.getShort();
196
197 icmp.payload = Data.deserializer()
198 .deserialize(data, bb.position(), bb.limit()
199 - bb.position());
200 icmp.payload.setParent(icmp);
201 return icmp;
202 };
alshabibc4901cd2014-09-05 16:50:40 -0700203 }
Jian Li5fc14292015-12-04 11:30:46 -0800204
205 @Override
206 public String toString() {
207 return toStringHelper(getClass())
208 .add("icmpType", Byte.toString(icmpType))
209 .add("icmpCode", Byte.toString(icmpCode))
210 .add("checksum", Short.toString(checksum))
211 .toString();
212 }
Pier Ventre78e73f62016-12-02 19:59:28 -0800213
214 /**
215 * Builds an ICMP reply using the supplied ICMP request.
216 *
217 * @param ethRequest the Ethernet packet containing the ICMP ECHO request
218 * @return the Ethernet packet containing the ICMP ECHO reply
219 */
220 public static Ethernet buildIcmpReply(Ethernet ethRequest) {
221
222 if (ethRequest.getEtherType() != Ethernet.TYPE_IPV4) {
223 return null;
224 }
225
226 IPv4 ipRequest = (IPv4) ethRequest.getPayload();
227
228 if (ipRequest.getProtocol() != IPv4.PROTOCOL_ICMP) {
229 return null;
230 }
231
232 Ethernet ethReply = new Ethernet();
233 IPv4 ipReply = new IPv4();
234
235 int destAddress = ipRequest.getDestinationAddress();
236 ipReply.setDestinationAddress(ipRequest.getSourceAddress());
237 ipReply.setSourceAddress(destAddress);
238 ipReply.setTtl((byte) 64);
Charles Chan83439a32017-10-24 15:21:16 -0700239 ipReply.setDiffServ(ipRequest.getDiffServ());
Pier Ventre78e73f62016-12-02 19:59:28 -0800240 ipReply.setChecksum((short) 0);
241 ipReply.setProtocol(IPv4.PROTOCOL_ICMP);
242
243 ICMP icmpRequest = (ICMP) ipRequest.getPayload();
244 ICMP icmpReply = new ICMP();
245
246 icmpReply.setPayload(icmpRequest.getPayload());
247 icmpReply.setIcmpType(ICMP.TYPE_ECHO_REPLY);
248 icmpReply.setIcmpCode(ICMP.SUBTYPE_ECHO_REPLY);
249 icmpReply.setChecksum((short) 0);
250 ipReply.setPayload(icmpReply);
251
252 ethReply.setEtherType(Ethernet.TYPE_IPV4);
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700253 ethReply.setQinQVID(ethRequest.getQinQVID());
254 ethReply.setQinQTPID(ethRequest.getQinQTPID());
Pier Ventre78e73f62016-12-02 19:59:28 -0800255 ethReply.setVlanID(ethRequest.getVlanID());
256 ethReply.setDestinationMACAddress(ethRequest.getSourceMACAddress());
257 ethReply.setSourceMACAddress(ethRequest.getDestinationMACAddress());
258 ethReply.setPayload(ipReply);
259
260
261 return ethReply;
262 }
alshabibc4901cd2014-09-05 16:50:40 -0700263}